457. Circular Array Loop #
Problem #
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?
Problem Summary #
Given a circular array nums containing positive and negative integers. If the number k at an index is positive, move forward k indices. Conversely, if it is negative (-k), move backward k indices. Because the array is circular, you may assume that the next element of the last element is the first element, and the previous element of the first element is the last element.
Determine whether there is a loop (or cycle) in nums. The loop must start and end at the same index and have a length > 1. Furthermore, all movements in a loop must proceed in the same direction. In other words, a loop cannot include both forward and backward movements.
Hints:
- -1000 ≤ nums[i] ≤ 1000
- nums[i] ≠ 0
- 1 ≤ nums.length ≤ 5000
Follow-up:
- Can you write an algorithm with time complexity O(n) and extra space complexity O(1)?
Solution Approach #
- Given a circular array, the numbers in the array represent the number of steps to move forward or backward.
+means moving right (forward), and-means moving left (backward). Determine whether there is a loop in this circular array, and the loop cannot contain only one element; all directions within the loop must be the same. - When encountering a cycle, you can first consider using the fast and slow pointer method to detect it. This problem adds one condition to the cycle: the cycle cannot be a single-element cycle, so add this check to the fast and slow pointer logic. Another condition is that the direction of the cycle must be the same. This is simple: use
num[i] * num[j] > 0to determine whether they are in the same direction (if they are in opposite directions, their product must be negative). If no cycle is found, set allnum[]values along the path already traversed to 0, marking them as visited. The next time the loop encounters a visited element,num[i] * num[j] > 0will be 0, and the cycle-finding process will exit early.
Code #
package leetcode
func circularArrayLoop(nums []int) bool {
if len(nums) == 0 {
return false
}
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
continue
}
// slow/fast pointer
slow, fast, val := i, getNextIndex(nums, i), 0
for nums[fast]*nums[i] > 0 && nums[getNextIndex(nums, fast)]*nums[i] > 0 {
if slow == fast {
// check for loop with only one element
if slow == getNextIndex(nums, slow) {
break
}
return true
}
slow = getNextIndex(nums, slow)
fast = getNextIndex(nums, getNextIndex(nums, fast))
}
// loop not found, set all element along the way to 0
slow, val = i, nums[i]
for nums[slow]*val > 0 {
next := getNextIndex(nums, slow)
nums[slow] = 0
slow = next
}
}
return false
}
func getNextIndex(nums []int, index int) int {
return ((nums[index]+index)%len(nums) + len(nums)) % len(nums)
}