打家劫舍:
337. House Robber III
class Solution {
public:
int rob(TreeNode* root) {
vector<int> res = robTree(root);
return max(res[0], res[1]);
}
vector<int> robTree(TreeNode* cur){
if(cur == NULL) return vector<int> {0, 0};
vector<int> left = robTree(cur->left);
vector<int> right = robTree(cur->right);
//偷了的值
int val1 = cur->val + left[1] + right[1];
int val2 = max(left[1], left[0]) + max(right[1], right[0]);
return {val1, val2};
}
};
股票问题:
121. Best Time to Buy and Sell Stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<vector<int>> dp(prices.size(), vector<int>(2,0));
dp[0][1] = -prices[0];
for(int i=1; i<prices.size(); i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i]);
dp[i][1] = max(dp[i-1][1], - prices[i]);
}
return dp[prices.size()-1][0];
}
};
122. Best Time to Buy and Sell Stock II
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
vector<vector<int>> dp(size, vector<int>(2, 0));
dp[0][1] = -prices[0];
for(int i=1; i<size; i++){
dp[i][0] = max(dp[i-1][0], dp[i-1][1]+prices[i]);
dp[i][1] = max(dp[i-1][1], dp[i-1][0]-prices[i]);
}
return dp[size-1][0];
}
};
123. Best Time to Buy and Sell Stock III
class Solution {
public:
int maxProfit(vector<int>& prices) {
//5种状态
/*
1.无操作
2.第一次持有
3.第一次不持有
4.第二次持有
5.第二次不持有
*/
int size = prices.size();
vector<vector<int>> dp(size, vector<int>(5, 0));
dp[0][1] = dp[0][3] = -prices[0];
for(int i=1; i<size; i++){
dp[i][1] = max(dp[i-1][1], - prices[i]);
dp[i][2] = max(dp[i-1][2], dp[i-1][1] + prices[i]);
dp[i][3] = max(dp[i-1][3], dp[i-1][2] - prices[i]);
dp[i][4] = max(dp[i-1][4], dp[i-1][3] + prices[i]);
}
return dp[size-1][4];
}
};
188. Best Time to Buy and Sell Stock IV
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int size = prices.size();
vector<vector<int>> dp(size, vector<int>(2*k+1, 0));
for(int i=1; i<2*k+1; i++){
if(i%2 == 1)
dp[0][i] = -prices[0];
}
for(int i=1; i<size; i++){
for(int j=1; j<2*k+1; j++){
if(j%2 == 1){
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]-prices[i]);
}else{
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]+prices[i]);
}
}
}
return dp[size-1][2*k];
}
};
子序列问题:
300. Longest Increasing Subsequence
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() == 1) return 1;
vector<int> dp(nums.size(), 1);
int res = 0;
for(int i=1; i<nums.size(); 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;
}
};
dp[i] 表示下标为i的数字(包括)的前面的最长子序列的长度
因为这里最长子序列不一定在最后,所以要重新设一个值记录最长子序列
初始化的时候所有的值的初始值都应该是1
674. Longest Continuous Increasing Subsequence
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
vector<int> dp(nums.size(), 1);
int res = 1;
for(int i=1; i<nums.size(); i++){
if(nums[i] > nums[i-1]){
dp[i] = dp[i-1]+1;
}
if(dp[i] > res) res = dp[i];
}
return res;
}
};
718. Maximum Length of Repeated Subarray
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> dp(nums1.size()+1, vector<int>(nums2.size()+1, 0));
//dp数组的含义:nums1中下标为i-1和nums2下标为j-1的数的前面的最长相同子序列
int res = 0;
for(int i=1; i<=nums1.size(); i++){
for(int j=1; j<=nums2.size(); j++){
if(nums1[i-1] == nums2[j-1]){
dp[i][j] = dp[i-1][j-1] +1;
}
if(res < dp[i][j]) res = dp[i][j];
}
}
return res;
}
};
1143. Longest Common Subsequence
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.size()+1, vector<int>(text2.size()+1, 0));
//在长度text1【0,i-1】和text2长度【0.j-1】中最长的公共子序列
for(int i=1;i<=text1.size(); i++){
for(int j=1; j<=text2.size(); j++){
if(text1[i-1] == text2[j-1]){
dp[i][j] = dp[i-1][j-1]+1;
}else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[text1.size()][text2.size()];
}
};
一定要注意好下标
1035. Uncrossed Lines
class Solution {
public:
int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> dp(nums1.size()+1, vector<int>(nums2.size()+1, 0));
for(int i=1; i<=nums1.size(); i++){
for(int j=1; j<=nums2.size(); j++){
if(nums1[i-1] == nums2[j-1]){
dp[i][j] = dp[i-1][j-1]+1;
}else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[nums1.size()][nums2.size()];
}
};