LeetCode:309. Best Time to Buy and Sell Stock with Cooldown

买卖股票系列的第5题。题目是:

Say you have an array for which the ith element is the price of a
given stock on day i.

Design an algorithm to find the maximum profit. You may complete as
many transactions as you like (ie, buy one and sell one share of the
stock multiple times) with the following restrictions:

You may not engage in multiple transactions at the same time (ie, you
must sell the stock before you buy again). After you sell your stock,
you cannot buy stock on next day. (ie, cooldown 1 day)

这里的特殊要求是卖出一次,需要等一天,才能重新买入。
题目不难,只是优化方面需要注意一下。

这题应该用动态规划来做。
状态定义:dp[i]:第i天能获得的最大利润
状态转移方程:dp[i] = max(dp[i-1], dp[j-2] + (prices[i] - prices[j])), 0 <= j < i,这里分别对应在第i天不卖和卖的情况。
很快写出代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.empty() || prices.size() == 1)
            return 0;
        int sz = prices.size();
        vector<int> dp(sz, 0);
        dp[1] = prices[1] > prices[0] ? prices[1] - prices[0] : 0;
        for (int i = 2; i < sz; ++i) {
            dp[i] = dp[i-1];
            for (int j = 0; j < i; ++j) {
                int temp = j-2 < 0 ? prices[i] - prices[j] : dp[j-2] + prices[i] - prices[j];
                if (temp > dp[i])
                    dp[i] = temp;
            }
        }
        return dp.back();
    }
};

代码写成这样会有效率低的问题。
我们观察状态转移方程,将后半部分化为:(dp[j-2] - prices[j]) + prices[i],
对于不同的i,(dp[j-2] - prices[j])的前面部分部分是一样的,也就是上面的代码重复计算了这一部分。进行优化后的代码是:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.empty() || prices.size() == 1)
            return 0;
        int sz = prices.size();
        vector<int> dp(sz, 0);
        dp[1] = prices[1] > prices[0] ? prices[1] - prices[0] : 0;
        int tempMax = max(-prices[0], -prices[1]);//tempMax = dp[j-2] - prices[j]
        for (int i = 2; i < sz; ++i) {
            tempMax = max(tempMax, dp[i-2] - prices[i]);
            dp[i] = max(dp[i-1], tempMax + prices[i]);
        }
        return dp.back();
    }
};

时间复杂度由原来的O(n^2)降低到O(n),优化的结果还行

猜你喜欢

转载自blog.csdn.net/weixin_43462819/article/details/83271090