[leetcode daily question] 565 array nesting

Insert image description here

Thought process:

Idea v1.0

  1. First learn to write s[0] and use an ans array to receive elements. Every time you add to ans, make a judgment first.
    • Will this index exceed the length of the array?
    • Is there this element in ans?
  2. After writing s[0], use a for loop to calculate all s[i]. Each time it is calculated, it is compared with the maximum length to maintain the maximum length.

code show as below:


/**
 * @param {number[]} nums
 * @return {number}
 */
const getILen = (nums,i) =>{
    
    
    let arr = []
    arr.push(nums[i])
    let index = arr[arr.length-1]
    
    while(index<nums.length && !arr.includes(nums[index])){
    
    
        arr.push(nums[index])
        index = arr[arr.length-1]
    }
    return arr.length
}
var arrayNesting = function(nums) {
    
    
    let max = 0;
    for(let i=0;i<nums.length;i++){
    
    
        max = Math.max(max,getILen(nums,i))
    }
    return max;
};

Idea v1.1

Since index is num[i] and the prompt says 0≤ nums[i]<n , there is no need to consider the possibility of index benefit.

code show as below:


/**
 * @param {number[]} nums
 * @return {number}
 */
const getILen = (nums,i) =>{
    
    
    let arr = []
    arr.push(nums[i])
    let index = arr[arr.length-1]
    
    while(!arr.includes(nums[index])){
    
    
        arr.push(nums[index])
        index = arr[arr.length-1]
    }
    return arr.length
}
var arrayNesting = function(nums) {
    
    
    let max = 0;
    for(let i=0;i<nums.length;i++){
    
    
        max = Math.max(max,getILen(nums,i))
    }
    return max;
};

Insert image description here

Idea v2.0

You need to traverse arr every time, and the time complexity is very high, so it can be optimized.

Go to arr and traverse → Every time you put an element in the nums array into arr, change this element to -1 at the same time. The next time you get it, you will not get it if you find it is -1. When a for iteration ends, the nums array is restored.

/**
 * @param {number[]} nums
 * @return {number}
 */
const getILen = (nums,i) =>{
    
    
    let arr = []
    let temp = [...nums];
    while(temp[i]!=-1){
    
    
        arr.push(temp[i]);
        let index = temp[i];
        temp[i] = -1;
        i = index
    }
    return arr.length
}
var arrayNesting = function(nums) {
    
    
    let max = 0;
    for(let i=0;i<nums.length;i++){
    
    
        max = Math.max(max,getILen(nums,i))
    }
    return max;
};

Insert image description here

From 854→869

Idea v2.1

Use arr to store, and then calculate arr.length. Just to calculate the length, you can optimize the point and use count.

/**
 * @param {number[]} nums
 * @return {number}
 */
const getILen = (nums,i) =>{
    
    
    let count = 0
    let temp = [...nums];
    while(temp[i]!=-1){
    
    
        count++;
        let index = temp[i];
        temp[i] = -1;
        i = index
    }
    return count
}
var arrayNesting = function(nums) {
    
    
    let max = 0;
    for(let i=0;i<nums.length;i++){
    
    
        max = Math.max(max,getILen(nums,i))
    }
    return max;
};

Insert image description here

From 869→875

Idea v3.0

because

 let temp = [...nums];

The time complexity is O(N), so it will still time out.

Looking at the solution, I found that this step was omitted. I originally thought that if this step was omitted, the original nums array would be modified. This will cause the damaged array to be used the next time you enter the for iteration.

After thinking about it for a long time, I discovered that the next for loop iteration will not fetch the elements in the last for iteration. The reasons are as follows:

Insert image description here

If the data of the first iteration is used in subsequent iterations, it will also be a repeated link.

s[0] : 0→5→6→2→0

s[2] : 2→0→5→6→2

Reason: There is no duplication of arr elements. If you want to get a certain element, you can only enter from the same element. Therefore, as long as an element has been traversed once in a certain iteration and encountered again in the next iteration, the set obtained will be the same, so this iteration can be skipped.

Therefore, the original array is directly destroyed without entering the iteration! !

Insert image description here

Guess you like

Origin blog.csdn.net/qq_43720551/article/details/134656216