-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0457-circular-array-loop.py
More file actions
46 lines (40 loc) · 1.41 KB
/
0457-circular-array-loop.py
File metadata and controls
46 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# time complexity: O(n^2)
# space complexity: O(1)
from typing import List
class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
def nextStep(currIdx: int, value: int, size: int):
result = (currIdx + value) % size
if result < 0:
result += size
return result
def isNotCycle(nums, prevDirection, currIdx):
currDirection = nums[currIdx] > 0
if prevDirection != currDirection:
return True
if abs(nums[currIdx] % len(nums)) == 0:
return True
return False
n = len(nums)
for i in range(n):
slow = fast = i
forward = nums[i] > 0
while True:
slow = nextStep(slow, nums[slow], n)
if isNotCycle(nums, forward, slow):
break
fast = nextStep(fast, nums[fast], n)
if isNotCycle(nums, forward, fast):
break
fast = nextStep(fast, nums[fast], n)
if isNotCycle(nums, forward, fast):
break
if slow == fast:
return True
return False
nums = [2, -1, 1, 2, 2]
print(Solution().circularArrayLoop(nums))
nums = [-1, -2, -3, -4, -5, 6]
print(Solution().circularArrayLoop(nums))
nums = [1, -1, 5, 1, 4]
print(Solution().circularArrayLoop(nums))