from typing import List class Solution: def solve(self, arr: List[int]) -> int: n = len(arr) vis = [0] * n for i in range(n): if vis[i]: continue # res += 1 while arr[i] != i: vis[arr[i]] = 1 arr[i], arr[arr[i]] = arr[arr[i]], arr[i] print(arr) vis[i] = 1 return arr s: Solution = Solution() cur = s.solve([1, 0]) print(cur)
I am getting TLE for this. the expecatation was [0, 1]. I know something is off in swapping but i am unable to understand please help
submitted by /u/AggravatingParsnip89
[link] [comments]
r/learnpython from typing import List class Solution: def solve(self, arr: List[int]) -> int: n = len(arr) vis = [0] * n for i in range(n): if vis[i]: continue # res += 1 while arr[i] != i: vis[arr[i]] = 1 arr[i], arr[arr[i]] = arr[arr[i]], arr[i] print(arr) vis[i] = 1 return arr s: Solution = Solution() cur = s.solve([1, 0]) print(cur) I am getting TLE for this. the expecatation was [0, 1]. I know something is off in swapping but i am unable to understand please help submitted by /u/AggravatingParsnip89 [link] [comments]
from typing import List class Solution: def solve(self, arr: List[int]) -> int: n = len(arr) vis = [0] * n for i in range(n): if vis[i]: continue # res += 1 while arr[i] != i: vis[arr[i]] = 1 arr[i], arr[arr[i]] = arr[arr[i]], arr[i] print(arr) vis[i] = 1 return arr s: Solution = Solution() cur = s.solve([1, 0]) print(cur)
I am getting TLE for this. the expecatation was [0, 1]. I know something is off in swapping but i am unable to understand please help
submitted by /u/AggravatingParsnip89
[link] [comments]