300. The longest increasing subsequence - LeetCode
status: AC
It is possible to delete some elements in a subsequence without changing the original order. A double traversal is required for judgment. The code is as follows:
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int len = nums.size(), res = 1;
vector<int> dp(len, 1);
for(int i = 1; i < len; ++i){
for(int j = 0; j < i; ++j){
if(nums[i] > nums[j]){
dp[i] = max(dp[i], dp[j]+1);
}
}
if(dp[i] > res) res = dp[i];
}
return res;
}
};
674. The longest continuous increasing sequence - LeetCode
status: AC
The increasing sequence is a continuous subsequence. A little trick is to put the final answer in a loop for updating. The code is as follows:
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int len = nums.size(), res = 1;
vector<int> dp(len, 1);
for(int i = 1; i < len; ++i){
if(nums[i] > nums[i-1]){
dp[i] = dp[i-1] + 1;
}
res = max(res, dp[i]);
}
return res;
}
};
An improved version of the previous question, the code is as follows:
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size(), len2 = nums2.size(), res = 0;
vector<vector<int>> dp(len1+1, vector<int>(len2+1, 0));
for(int i = 1; i <= len1; ++i){
for(int j = 1; j <= len2; ++j){
if(nums1[i-1] == nums2[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
}
res = max(res, dp[i][j]);
}
}
return res;
}
};