Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/457.html
457. Circular Array Loop (Medium)
You are given a circular array nums
of positive and negative integers. If a number k at an index is positive, then move forward k steps. Conversely, if it's negative (-k), move backward k steps. Since the array is circular, you may assume that the last element's next element is the first element, and the first element's previous element is the last element.
Determine if there is a loop (or a cycle) in nums
. A cycle must start and end at the same index and the cycle's length > 1. Furthermore, movements in a cycle must all follow a single direction. In other words, a cycle must not consist of both forward and backward movements.
Example 1:
Input: [2,-1,1,2,2] Output: true Explanation: There is a cycle, from index 0 -> 2 -> 3 -> 0. The cycle's length is 3.
Example 2:
Input: [-1,2] Output: false Explanation: The movement from index 1 -> 1 -> 1 ... is not a cycle, because the cycle's length is 1. By definition the cycle's length must be greater than 1.
Example 3:
Input: [-2,1,-1,-2,-2] Output: false Explanation: The movement from index 1 -> 2 -> 1 -> ... is not a cycle, because movement from index 1 -> 2 is a forward movement, but movement from index 2 -> 1 is a backward movement. All movements in a cycle must follow a single direction.
Note:
- -1000 ≤ nums[i] ≤ 1000
- nums[i] ≠ 0
- 1 ≤ nums.length ≤ 5000
Follow up:
Could you solve it in O(n) time complexity and O(1) extra space complexity?
Related Topics:
Array, Two Pointers
Solution 1.
-
class Solution { public boolean circularArrayLoop(int[] nums) { int length = nums.length; int[] hasLoop = new int[length]; for (int i = 0; i < length; i++) { if (hasLoop[i] == 0) { boolean loop = checkLoop(nums, hasLoop, i); if (loop) return true; } } return false; } public boolean checkLoop(int[] nums, int[] hasLoop, int startIndex) { int length = nums.length; List<Integer> indices = new ArrayList<Integer>(); indices.add(startIndex); int index = startIndex; int direction = 0; do { int num = nums[index]; if (direction == 0) direction = num / Math.abs(num); else if (direction * num < 0) return false; index = (index + num) % length; if (index < 0) index += length; int occur = indices.indexOf(index); if (occur < 0) indices.add(index); else { int size = indices.size(); if (occur == size - 1) { for (int curIndex : indices) hasLoop[curIndex] = -1; return false; } else { for (int i = 0; i < occur; i++) hasLoop[indices.get(i)] = -1; for (int i = occur; i < size; i++) hasLoop[indices.get(i)] = 1; return true; } } } while (index != startIndex); return false; } } ############ class Solution { private int n; private int[] nums; public boolean circularArrayLoop(int[] nums) { n = nums.length; this.nums = nums; for (int i = 0; i < n; ++i) { if (nums[i] == 0) { continue; } int slow = i, fast = next(i); while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(fast)] > 0) { if (slow == fast) { if (slow != next(slow)) { return true; } break; } slow = next(slow); fast = next(next(fast)); } int j = i; while (nums[j] * nums[next(j)] > 0) { nums[j] = 0; j = next(j); } } return false; } private int next(int i) { return (i + nums[i] % n + n) % n; } }
-
// OJ: https://leetcode.com/problems/circular-array-loop/ // Time: O(N^2) // Space: O(1) class Solution { inline int go(vector<int> &A, int i) { return (i + A[i] + A.size()) % A.size(); } inline bool sameSign(int a, int b) { return (a > 0 && b > 0) || (a < 0 && b < 0); } public: bool circularArrayLoop(vector<int>& A) { int N = A.size(); for (int i = 0; i < N; ++i) { int fast = i, slow = i, valid = 1; do { fast = go(A, fast); if (!sameSign(A[fast], A[i])) { valid = 0; break; } fast = go(A, fast); if (!sameSign(A[fast], A[i])) { valid = 0; break; } slow = go(A, slow); } while (fast != slow); if (valid && slow != go(A, slow)) return true; } return false; } };
-
class Solution: def circularArrayLoop(self, nums: List[int]) -> bool: n = len(nums) def next(i): return (i + nums[i] % n + n) % n for i in range(n): if nums[i] == 0: continue slow, fast = i, next(i) while nums[slow] * nums[fast] > 0 and nums[slow] * nums[next(fast)] > 0: if slow == fast: if slow != next(slow): return True break slow, fast = next(slow), next(next(fast)) j = i while nums[j] * nums[next(j)] > 0: nums[j] = 0 j = next(j) return False ############ class Solution(object): def circularArrayLoop(self, nums): """ :type nums: List[int] :rtype: bool """ N, self.nums = len(nums), nums for i in range(N): slow = i fast = self.nextpos(slow) while nums[fast] * nums[i] > 0 and nums[self.nextpos(fast)] * nums[i] > 0: if fast == slow: if slow == self.nextpos(slow): break return True slow = self.nextpos(slow) fast = self.nextpos(self.nextpos(fast)) return False def nextpos(self, index): N = len(self.nums) return (index + self.nums[index] + N) % N
-
func circularArrayLoop(nums []int) bool { for i, num := range nums { if num == 0 { continue } slow, fast := i, next(nums, i) for nums[slow]*nums[fast] > 0 && nums[slow]*nums[next(nums, fast)] > 0 { if slow == fast { if slow != next(nums, slow) { return true } break } slow, fast = next(nums, slow), next(nums, next(nums, fast)) } j := i for nums[j]*nums[next(nums, j)] > 0 { nums[j] = 0 j = next(nums, j) } } return false } func next(nums []int, i int) int { n := len(nums) return (i + nums[i]%n + n) % n }