O(n) time, O(1) place, Python beats 97.38%


  • 0
    E

    Although it's double circulation, actually, each number is traversed just once. So it's still O(n) runtime

    class Solution(object):
        def arrayNesting(self, nums):
            l,m=len(nums),0
            for i in range(l):
                if nums[i]==-1:continue
                if m>=l-m:return m
                j,c=nums[i],0
                while nums[j]!=-1:
                    c+=1
                    t=nums[j]
                    nums[j],j=-1,t
                if c>m:m=c
            return m
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.