알고리즘/Leetcode

[leetcode/python3] 565. Array Nesting

llollhh_ 2021. 9. 18. 14:21

https://leetcode.com/problems/array-nesting/

 

Array Nesting - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution:
    def arrayNesting(self, nums: List[int]) -> int:
        result = 0
        
        for index in range(0, len(nums)):
            length = 0
            while nums[index] != -1:
                length += 1
                
                temp = nums[index]
                nums[index] = -1
                index = temp
            else:
                result = max(result, length)
        
        return result