leetcode300. The longest increasing subsequence

topic

Link: https://leetcode-cn.com/problems/longest-increasing-subsequence/

 

300. The longest increasing subsequence

Medium difficulty 1333

Given an array of integers  nums , find the length of the longest strictly increasing subsequence.

A subsequence is a sequence derived from an array, deleting (or not deleting) elements in the array without changing the order of the remaining elements. For example, it [3,6,2,7] is [0,3,1,6,2,2,7] a subsequence of an array  .

 

Example 1:

Input: nums = [10,9,2,5,3,7,101,18]
 Output: 4
 Explanation: The longest increasing subsequence is [2,3,7,101], so the length is 4.

Example 2:

Input: nums = [0,1,0,3,2,3]
 Output: 4

Example 3:

Input: nums = [7,7,7,7,7,7,7]
 Output: 1

analysis

    Suggested reference: https://blog.csdn.net/mk33092250/article/details/113862228

     Note: The general problem of finding the maximum value of a sub-array can be converted through a one-dimensional array. When encountering the problem of the maximum value of an array, you can give priority to whether it can be obtained through a one-dimensional array conversion.

    The idea is as follows:

1. Solve the increasing subsequence and convert it to save the increasing subsequence corresponding to each index position through a one-dimensional array

2. Because the number of each position must be greater than or equal to 1, the default value is filled with 1.

int[] countArray=new int[nums.length];
Arrays.fill(countArray,1);

3. How to determine the longest increasing subsequence at each position? The current position is compared with countArray. If the current element is greater than the previous element, and the number of current elements is less than the previous element, then the value of the current element is set to the value of the previous element + 1, which is not easy to understand. Let’s take a complete example. .

For example

Take [10,9,2,5,3,7,101,18] as an example

  1. Copy the original array countArray, the default value is filled with 1, and countArray is now [1,1,1,1,1,1,1,1];
  2. Start traversing from the second position, the first position is always 1, the second position index=1, at this time 9<10, so the value of the second position of countArray is still 1.
  3. In the third position, 2 is compared with 10, and 2 is compared with 9, and the third position is also 1.
  4. The fourth position, 5 and 10 are compared, 5 and 9 are not greater than the comparison, when 5 and 2 are compared, 5>2, at this time, countArray[3]=countArray[2]+1, (countArray of 3 and 2 The indexes corresponding to the original arrays 5 and 2 respectively) countArray is now [1,1,1,2,1,1,1,1]
  5. The fifth position, 3>2, at this time countArray[4]=countArray[2]+1, and countArray at this time is [1,1,1,2,2,1,1,1]
  6. The sixth position, 7>2, at this time countArray[5]=countArray[2]+1, and countArray at this time is [1,1,1,2,2,2,1,1]
  7. The sixth position, 7>5, at this time countArray[5]=countArray[3]+1, and countArray at this time is [1,1,1,2,2,3,1,1]
  8. The sixth position, 7>3, at this time countArray[5]=countArray[4]+1, and countArray at this time is [1,1,1,2,2,3,1,1]
  9. . . . By analogy, we finally get the complete countArray
  10. Traverse countArray and get the maximum value, which is the largest increasing subsequence

Code

package org.example;

import java.util.Arrays;

public class Leetcode300 {
    public static void main(String[] args) {
        Leetcode300 leetcode300 = new Leetcode300();
        //int[] param=new int[]{10,9,2,5,3,7,101,18};
        int[] param=new int[]{10,9,2,5,3,7,101,18,19,20};
        System.out.println(leetcode300.lengthOfLIS(param) );;
    }

    public int lengthOfLIS(int[] nums) {
        int[] countArray=new int[nums.length];
        Arrays.fill(countArray,1);
        int result=1;
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i]>nums[j]&&countArray[i]<=countArray[j]){
                    countArray[i]=countArray[j]+1;
                    if (countArray[i]>result){
                        result=countArray[i];
                    }
                }
            }
        }
        return result;
    }
}

 

Guess you like

Origin blog.csdn.net/mk33092250/article/details/113862996