LeetCode 457. Circular Array Loop

Problem Statement

(Source) You are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it’s negative (-n), move backward n steps. Assume the first element of the array is forward next to the last element, and the last element is backward next to the first element. Determine if there is a loop in this array. A loop starts and ends at a particular index with more than 1 element along the loop. The loop must be “forward” or “backward’.

Example 1: Given the array [2, -1, 1, 2, 2], there is a loop, from index 0 -> 2 -> 3 -> 0.

Example 2: Given the array [-1, 2], there is no loop.

Note: The given array is guaranteed to contain no element “0”.

Can you do it in O(n) time complexity and O(1) space complexity?

Solution

class Solution(object):
    def circularArrayLoop(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        n = len(nums)
        remain = n
        for i in xrange(n):
            if nums[i] != None:
                # Test if starting from position i, the subsequent numbers can form
                # a circular array or not.
                j = i
                count = 1
                while True:
                    jj = j
                    j = (j + nums[j] + n) % n
                    if jj == j:
                        break
                    count += 1
                    if nums[j] == None or nums[j] * nums[i] < 0:
                        break
                    # If there is a circular loop, the count of elements of the loop
                    # will definitely approach the possible maximum size (original 
                    # array size - those marked as None) at some point.
                    if count > remain:
                        return True
                # Starting from position i, the subsequent numbers cannot form
                # a circular array, mark all of them as None.
                j = i
                numsi = nums[i]
                while nums[j] != None:
                    jj = j
                    remain -= 1
                    j = (j + nums[j] + n) % n
                    nums[jj] = None
                    if nums[j] == None or nums[j] * numsi < 0:
                        break
        return False

Complexity Analysis:

  • Time Complexity: O(n) . Each element in the input array nums can be visited at most three times within the while loop. Because when some element x is encountered for the second time, either it is an indication that a circular array loop cannot be formed (one more visiting time is needed to mark it as None), or a circular array loop has been formed (exit for loop and return the result).
  • Space Complexity: O(1) .
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值