题源:LeetCode

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

最长递增子序列_其他

 

这类动态规划题目就是考虑目前和之前的大小关系

 1 class Solution {
 2 public:
 3     int lengthOfLIS(vector<int>& nums) {
 4         int n = (int)nums.size();
 5         if (n == 0) {
 6             return 0;
 7         }
 8         vector<int> dp(n, 0);
 9         for (int i = 0; i < n; ++i) {
10             dp[i] = 1;
11             for (int j = 0; j < i; ++j) {
12                 if (nums[j] < nums[i]) {
13                     dp[i] = max(dp[i], dp[j] + 1);
14                 }
15             }
16         }
17         return *max_element(dp.begin(), dp.end());
18     }
19 };