原题链接:https://leetcode.com/problems/longest-increasing-subsequence
原题意大意是给定一个数字序列,相当于是一个数组,然后求出最长递增子序列的长度,这道题刚入手就可以大致确定解法思路,是利用DP动态规划来实现的,利用一个数组L(i)来存放当前以第i个数字结尾的最长子序列的长度,通过所有与i相连的j,L(i)= 1+max{L(j)},其中j是比i小的数对应的下标。
源代码如下所示:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> res;
for(int i=0; i<nums.size(); i++) {
auto it = std::lower_bound(res.begin(), res.end(), nums[i]);
if(it==res.end()) res.push_back(nums[i]);
else *it = nums[i];
}
return res.size();
}
};