Leetcode 动态规划 打家劫舍 337 股票 121 122 123 188 子序列 300 674 718 1143 1035

打家劫舍:

337. House Robber III

class Solution {
public:
    int rob(TreeNode* root) {
        vector res = robTree(root);
        return max(res[0], res[1]);
    }

    vector robTree(TreeNode* cur){
        if(cur == NULL) return vector {0, 0};
        vector left = robTree(cur->left);
        vector 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& prices) {
        vector> dp(prices.size(), vector(2,0));

        dp[0][1] = -prices[0];

        for(int i=1; i

122. Best Time to Buy and Sell Stock II

class Solution {
public:
    int maxProfit(vector& prices) {
        int size = prices.size();
        vector> dp(size, vector(2, 0));

        dp[0][1] = -prices[0];

        for(int i=1; i

123. Best Time to Buy and Sell Stock III

class Solution {
public:
    int maxProfit(vector& prices) {
        //5种状态
        /*
        1.无操作
        2.第一次持有
        3.第一次不持有
        4.第二次持有
        5.第二次不持有
        */
        int size = prices.size();
        vector> dp(size, vector(5, 0));
        dp[0][1] = dp[0][3] = -prices[0];
        for(int i=1; i

188. Best Time to Buy and Sell Stock IV

class Solution {
public:
    int maxProfit(int k, vector& prices) {
        int size = prices.size();
        vector> dp(size, vector(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

 子序列问题:

300. Longest Increasing Subsequence

class Solution {
public:
    int lengthOfLIS(vector& nums) {
        if(nums.size() == 1) return 1;
        vector dp(nums.size(), 1);
        int res = 0;
        for(int i=1; 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& nums) {
        vector dp(nums.size(), 1);
        int res = 1;

        for(int i=1; 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& nums1, vector& nums2) {
        vector> dp(nums1.size()+1, vector(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> dp(text1.size()+1, vector(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& nums1, vector& nums2) {
        vector> dp(nums1.size()+1, vector(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()];
    }
};

你可能感兴趣的:(leetcode,动态规划,算法)