leetcode_309买卖股票最佳期(含冻结期)解题思路

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

// 方法一:暴力求解法,深度优先搜索,遍历所有情况,通过199/201条用例,出现TLE问题
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() < 2) return 0;
        int max = 0,sum = 0, i = 0,flag = -1;    // 1 means sell, -1 means buy
        bool cooldown = false;
        CalculateProfit(max, i, flag, sum, prices, cooldown);
        return max;
    }
    void CalculateProfit(int& max, int index, int flag, int sum, vector<int>& prices, bool cooldown) {
        if (index >= prices.size()) return;
        if (cooldown == true) {
            CalculateProfit(max, index + 1, -1, sum, prices, false);
            return;
        }

        // ought to buy
        if (flag == -1) {
            // don't buy this term
            CalculateProfit(max, index + 1, -1, sum, prices, false);
            // buy this term
            sum += prices[index] * flag;
            max = sum > max ? sum : max;
            CalculateProfit(max, index + 1, 1, sum, prices, false);
        }

        // ought to sell
        if (flag == 1)
        {
            // don't sell this term
            CalculateProfit(max, index + 1, 1, sum, prices, false);
            // sell this term
            sum += prices[index] * flag;
            sum = sum < 0 ? 0 : sum;
            max = sum > max ? sum : max;
            CalculateProfit(max, index + 1, -1, sum, prices, true);
        }
    }
};

// 方法二,动态规划(一维三因素动态规划),难在数字关系推导,理清三种状态的计算过程
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() < 2) return 0;
        vector<int> sell, buy, cool;
        sell.push_back(0);
        buy.push_back(-prices[0]);
        cool.push_back(0);
        int maxReturn = 0;
        for (int i = 1; i < prices.size(); ++i) {
            // sell
            sell.push_back(max(sell[i - 1], cool[i - 1]));
            maxReturn = sell.back() > maxReturn ? sell.back() : maxReturn;
            // buy
            buy.push_back(max(sell[i - 1] - prices[i], buy[i - 1]));
            // maxReturn = buy.back() > maxReturn ? buy.back() : maxReturn; (最大值不可能为此种状态,故可注释掉)
            // cool
            cool.push_back(buy[i - 1] + prices[i]);
            maxReturn = cool.back() > maxReturn ? cool.back() : maxReturn;
        }
        return maxReturn;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,买卖股票的最佳时机可以使用动态规划算法来解决。动态规划是一种通过将问题分解为子问题并利用子问题的解来求解原问题的方法。在这个问题中,我们可以定义一个状态数组dp,其中dp\[i\]表示第i天卖出股票所能获得的最大利润。然后,我们可以通过遍历价格数组prices,计算出每一天卖出股票所能获得的最大利润,并更新dp数组。最后,dp数组中的最后一个元素即为所求的最大利润。 根据引用\[3\]的内容,区间动态规划算法是解决买卖股票的最佳时机问题的一种方法。在这个算法中,我们需要考虑每个可能的买入和卖出的时间点,并计算出每个时间点的最大利润。然后,我们可以通过比较这些最大利润,找到最大的利润作为结果。 所以,买卖股票的最佳时机可以使用区间动态规划算法来解决。 #### 引用[.reference_title] - *1* *3* [【算法-LeetCode】121. 买卖股票的最佳时机(动态规划;贪心)](https://blog.csdn.net/qq_44879358/article/details/120306943)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【算法-LeetCode】122. 买卖股票的最佳时机 II(动态规划;贪心)](https://blog.csdn.net/qq_44879358/article/details/120783546)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值