Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    565 - Array Nesting\.

    Medium

    You are given an integer array nums of length n where nums is a permutation of the numbers in the range [0, n - 1].

    You should build a set s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... } subjected to the following rule:

    • The first element in s[k] starts with the selection of the element nums[k] of index = k.

    • The next element in s[k] should be nums[nums[k]], and then nums[nums[nums[k]]], and so on.

    • We stop adding right before a duplicate element occurs in s[k].

    Return the longest length of a set s[k].

    Example 1:

    Input: nums = 5,4,0,3,1,6,2

    Output: 4

    Explanation: nums0 = 5, nums1 = 4, nums2 = 0, nums3 = 3, nums4 = 1, nums5 = 6, nums6 = 2. One of the longest sets sk: s0 = {nums0, nums5, nums6, nums2} = {5, 6, 2, 0}

    Example 2:

    Input: nums = 0,1,2

    Output: 1

    Constraints:

    • <code>1 <= nums.length <= 10<sup>5</sup></code>

    • 0 &lt;= nums[i] &lt; nums.length

    • All the values of nums are unique.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer arrayNesting(IntArray nums)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait