300. Longest Increasing Subsequence

300. Longest Increasing Subsequence_第1张图片
image.png

参考: https://algorithm.yuanbin.me/zh-hans/dynamic_programming/longest_increasing_subsequence.html
300. Longest Increasing Subsequence_第2张图片
image.png

class Solution {
public:
    int lengthOfLIS(vector& nums) {
        vector dp(nums.size(), 1);
        int n = nums.size();
        int max = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < i; j++){
                if(nums[j] < nums[i] && dp[j] >= dp[i]){
                    dp[i] = dp[j] + 1;
                }
            }
            if(dp[i] >max) max = dp[i];
        }
        return max;
    }
};

你可能感兴趣的:(300. Longest Increasing Subsequence)