代码随想录算法训练营Day41 | 121. 买卖股票的最佳时机 | 122.买卖股票的最佳时机II | 123.买卖股票的最佳时机III

今日任务

121. 买卖股票的最佳时机

  • 题目链接: https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/
  • 题目描述
    在这里插入图片描述

Code

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        
        // dp[i][true] = max(dp[i - 1][true], - prices[i]);
        // dp[i][false] = max(dp[i - 1][false], dp[i - 1][true] + prices[i]);

        int f0 = 0, f1 = INT_MIN;
        for(int t : prices){
            int newf0 = max(f0, f1 + t);
            f1 = max(f1, -t);
            f0 = newf0;
        }
        return f0;
    }
};

122.买卖股票的最佳时机II

  • 题目链接: https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/
  • 题目描述
    在这里插入图片描述

Code

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // int profit = 0;
        // strong_ordering cmp_less = strong_ordering::less;
        // strong_ordering cmp_greater = strong_ordering::greater;
        // int buy = 0;
        // bool is_buy = false;
        // for(int i = 1; i < prices.size(); i++){
        //     if(!is_buy && prices[i - 1] <=> prices[i] == cmp_less){
        //         buy = prices[i - 1];
        //         is_buy = true;
        //     }else if(is_buy && prices[i - 1] <=> prices[i] == cmp_greater){
        //         profit += prices[i - 1] - buy;
        //         is_buy = false;
        //     }
        // }
        // if(is_buy && prices.back() > buy){
        //     profit += prices.back() - buy;
        // }
        // return profit;

        // int ans = 0;
        // int pre = 0;
        // int n = prices.size();
        // bool flag = false;
        // for(int i = 1; i < n; i++){
        //     int a = prices[i - 1], b = prices[i];
        //     if(!flag && a < b){
        //         pre = a;
        //         flag = !flag;
        //     }else if(flag && a > b){
        //         ans += a - pre;
        //         flag = !flag;
        //     }
        // }
        // if(flag && prices.back() > pre){
        //     ans += prices.back() - pre;
        // }
        // return ans;


        // 状态机DP
        int n = prices.size();
        // vector<array<int, 2>> memo(n, {-1, -1});
        // function<int(int, bool)> dfs = [&](int i, bool hold)->int{
        //     if(i < 0){
        //         return hold ? INT_MIN : 0;
        //     }
        //     int & res = memo[i][hold];
        //     if(res != -1){
        //         return res;
        //     }
        //     if(hold){
        //         return res = max(dfs(i - 1, true), dfs(i - 1, false) - prices[i]);
        //     }
        //     return res = max(dfs(i - 1, false), dfs(i - 1, true) + prices[i]);
        // };
        // return dfs(n - 1, false);

        // vector<array<int, 2>> f(n + 1);
        // f[0][1] = INT_MIN; // 前一天不能操作
        // for(int i = 0; i < n; i++){
        //     f[i + 1][0] = max(f[i][0], f[i][1] + prices[i]);
        //     f[i + 1][1] = max(f[i][1], f[i][0] - prices[i]);
        // }
        // return f[n][0];

        int f0 = 0, f1 = INT_MIN;
        for(int x : prices){
            int newf0 = max(f0, f1 + x);
            f1 = max(f1, f0 - x);
            f0 = newf0;
        }
        return f0;
    }
};

123.买卖股票的最佳时机III

  • 题目链接: https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/description/
  • 题目描述
    在这里插入图片描述

Code

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<array<int, 2>> dp(4, {INT_MIN / 2, INT_MIN / 2});
        for(int j = 1; j <= 3; j++){
            dp[j][0] = 0;
        }
        for(int x : prices){
            for(int j = 3; j > 0; j--){
                dp[j][0] = max(dp[j][0], dp[j - 1][1] + x);
                dp[j][1] = max(dp[j][1], dp[j][0] - x);
            }
        }
        return dp[3][0];
    }
};
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值