Simple Solution - Using Visited Arrays - Beats 85%


  • 0
    int max_count = 0;
    public int ArrayNesting(int[] nums)
    {
        int[] General_visited = new int[nums.Length];
                
        for(int i=0; i<nums.Length; i++)
        {
            if(General_visited[i]==0)
            {
                int[] Current_visited = new int[nums.Length];
                ArrayNesting_helper(nums, General_visited, Current_visited, 0, i);
            }
        }
        return max_count;
    }
    private void ArrayNesting_helper(int[] nums, int[] general_visited, int[] current_visited, int current_count, int current_index)
    {
        while(current_visited[current_index]==0)
        {
            current_count++;
            max_count = Math.Max(max_count, current_count);
            current_visited[current_index] = 1;
            general_visited[current_index] = 1;
            current_index = nums[current_index];
        }
    }
    

Log in to reply
 

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