X Tutup
The Wayback Machine - https://web.archive.org/web/20200930072031/https://github.com/vJechsmayr/PythonAlgorithms/issues/36
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0457 - Circular Array Loop #36

Open
vJechsmayr opened this issue Sep 23, 2020 · 2 comments
Open

0457 - Circular Array Loop #36

vJechsmayr opened this issue Sep 23, 2020 · 2 comments

Comments

@vJechsmayr
Copy link
Owner

@vJechsmayr vJechsmayr commented Sep 23, 2020

Description of the 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:

  1. -1000 ≤ nums[i] ≤ 1000
  2. nums[i] ≠ 0
  3. 1 ≤ nums.length ≤ 5000

Follow up:
Could you solve it in O(n) time complexity and O(1) extra space complexity?

Code

class Solution:
    def circularArrayLoop(self, nums: List[int]) -> bool:

Link To The LeetCode Problem

LeetCode

@Apoorve73
Copy link

@Apoorve73 Apoorve73 commented Sep 24, 2020

I would like to solve this! Can you please assign ?

@Apoorve73
Copy link

@Apoorve73 Apoorve73 commented Sep 26, 2020

Thank you! Will work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.
X Tutup