The most classic single-string LIS series: 300. Longest increasing subsequence

topic

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

子序列is a sequence derived from an array that removes (or does not remove) elements from the array without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].

analyze

insert image description here

code

class Solution {
    
    
    public int lengthOfLIS(int[] nums) {
    
    
        int[] dp = new int[nums.length];
        dp[0] = 1;
        int res = dp[0];
        for (int i = 1; i < nums.length; i++) {
    
    
            int max = 0;
            for (int j = 0; j < i; j++) {
    
    
                if(nums[i] > nums[j]){
    
    
                    max = Math.max(max,dp[j]);
                }
            }
            dp[i] = max + 1;
            res = Math.max(res,dp[i]);
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/upset_poor/article/details/123229700