一.最长上升子序列
1.定义
LIS(i):表示以第i个数字为结尾的最长上升子序列的长度
LIS(i):表示在[0...i]的范围内,选择数字nums[i]可以获得的最长上升子序列的长度
LIS(i) = max(1 + LIS(j)) (i > j)
2.例题
300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.
Example:
Input:[10,9,2,5,3,7,101,18]
Output: 4 Explanation: The longest increasing subsequence is[2,3,7,101]
, therefore the length is4
.
Note:
class Solution {
public:
int lengthOfLIS(vector& nums) {
int n = nums.size();
if (n == 0) return 0;
vector memo(n+1, 1);
for(int i = 1; i < n; i++){
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]){
memo[i] = max(memo[i], 1 + memo[j]);
}
}
}
int res = memo[0];
for(int i = 1; i < n; i++){
res = max(res, memo[i]);
}
return res;
}
};
二.最长公共子序列问题
1.定义
给出两个字符串S1和S2,求这两个字符串的最长公共子序列的长度
LCS(m,n) 是 S1[0...m] 和 S2[0...n]的最长公共子序列的长度
S1[m] == S2[n]: LCS(m, n) = 1 + LCS(m-1, n-1)
S1[m] != S2[n]: LCS(m, n) = max(LCS(m-1, n), LCS(m, n-1))
#include
#include
#include
#include
using namespace std;
class Solution {
public:
int canPartition(vector& nums1,vector& nums2) {
int m = nums1.size();
int n = nums2.size();
vector > memo(m, vector(n, 0));
if(nums1[0] == nums2[0]) memo[0][0] = 1;
for(int i=1; i w;
w.push_back(1);
w.push_back(2);
w.push_back(3);
vector w2;
w2.push_back(1);
w2.push_back(2);
w2.push_back(3);
w2.push_back(4);
w2.push_back(2);
w2.push_back(3);
int res = solution.canPartition(w, w2);
printf("%d\n",res);
return 0;
}