Skip to content

Commit

Permalink
Add solution for Circular Array Loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacknahil committed Mar 2, 2024
1 parent 73041c6 commit 2c4e32b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions camp1/week3/leetcode/circular-array-loop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:

visited=defaultdict(int)
def nex(i):
return (i+nums[i])%len(nums)

for i in range(len(nums)):
if visited[i]==1:
continue
slow,fast=i,nex(i)
while nums[slow]*nums[fast]>0 and nums[slow]*nums[nex(fast)]>0:
visited[slow]=1
if slow==fast:
if slow!=nex(slow):
return True
break
slow=nex(slow)
fast=nex(nex(fast))
return False




0 comments on commit 2c4e32b

Please sign in to comment.