Easy slow fast JAVA 36ms solution (beats 90%)


  • 0
    G
    class Solution {
        public int arrayNesting(int[] nums) {
            boolean[] b = new boolean[nums.length];
            int max_len = 0;
            for(int i=0; i<nums.length; i++){
                if(b[i])
                    continue;
                int slow = nums[i];
                int fast = nums[slow];
                b[i] = true;
                int len = 1;
                while(nums[i]!=fast){
                    b[slow] = true;
                    b[fast] = true;
                    slow = nums[slow];
                    fast = nums[slow];
                    len++;
                }
                if(len > max_len){
                    max_len = len;
                }
            }
            
            return max_len;
        }
    }
    

Log in to reply
 

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