资讯专栏INFORMATION COLUMN

[LeetCode]Best Time to Buy and Sell Stock with Coo

xcc3641 / 653人阅读

摘要:分析因为当前日期买卖股票会受到之前日期买卖股票行为的影响,首先考虑到用解决。所以我们可以用两个数组分别记录当前持股跟未持股的状态。

Best Time to Buy and Sell Stock with Cooldown

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)

Example:

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
分析

因为当前日期买卖股票会受到之前日期买卖股票行为的影响,首先考虑到用DP解决。

这道题比较麻烦的是有个cooldown的限制,其实本质也就是买与卖之间的限制。对于某一天,股票有三种状态: buy, sell, cooldown, sell与cooldown我们可以合并成一种状态,因为手里最终都没股票,最终需要的结果是sell,即手里股票卖了获得最大利润。所以我们可以用两个DP数组分别记录当前持股跟未持股的状态。然后根据题目中的限制条件,理清两个DP数组的表达式。

对于当天最终未持股的状态,最终最大利润有两种可能,一是今天没动作跟昨天未持股状态一样,二是昨天持股了,今天卖了。所以我们只要取这两者之间最大值即可,表达式如下:

sellDp[i] = Math.max(sellDp[i - 1], buyDp[i - 1] + prices[i]);

对于当天最终持股的状态,最终最大利润有两种可能,一是今天没动作跟昨天持股状态一样,二是前天还没持股,今天买了股票,这里是因为cooldown的原因,所以今天买股要追溯到前天的状态。我们只要取这两者之间最大值即可,表达式如下:

buyDp[i] = Math.max(buyDp[i - 1], sellDp[i - 2] - prices[i]);

最终我们要求的结果是

sellDp[n - 1] 表示最后一天结束时手里没股票时的累积最大利润

当然,这里空间复杂度是可以降到O(1)的,具体见第二种代码实现。

复杂度

time: O(n), space: O(n)

代码
public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        
        // 表示当天最终未持股的情况下,当天结束后的累计最大利润
        int[] sellDp = new int[prices.length];
        // 表示当天最终持股的情况下,当天结束后的累计最大利润
        int[] buyDp = new int[prices.length];
        
        // 考虑初始情况
        buyDp[0] = -prices[0];
        sellDp[0] = 0;
        for (int i = 1; i < prices.length; i++) {
            sellDp[i] = Math.max(sellDp[i - 1], buyDp[i - 1] + prices[i]);
            if (i >= 2) {
                buyDp[i] = Math.max(buyDp[i - 1], sellDp[i - 2] - prices[i]);
            } else {
                buyDp[i] = Math.max(buyDp[i - 1], -prices[i]);
            }
        }
        return sellDp[prices.length - 1];
    }
}
代码(O(1) space)
public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
    
        int currBuy = -prices[0];
        int currSell = 0;
        int prevSell = 0;
        for (int i = 1; i < prices.length; i++) {
            int temp = currSell;
            currSell = Math.max(currSell, currBuy + prices[i]);
            if (i >= 2) {
                currBuy = Math.max(currBuy, prevSell - prices[i]);
            } else {
                currBuy = Math.max(currBuy, -prices[i]);
            }
            prevSell = temp;
        }
        return currSell;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65329.html

相关文章

  • LeetCode 309. Best Time to Buy and Sell Stock with

    摘要:示例输入输出解释对应的交易状态为买入卖出冷冻期买入卖出思路这道题使用动态规划。状态表示当天休息能够获得的最大价值,表示当天持有股票能够获得的最大价值,表示当天持有股票能够获得的最大价值。 Description Say you have an array for which the ith element is the price of a given stock on day i. ...

    刘明 评论0 收藏0
  • 309. Best Time to Buy and Sell Stock with Cooldown

    摘要:题目链接来解,要用两个分别表示现在的操作是还是,优化空间用滚动数组,或者几个 309. Best Time to Buy and Sell Stock with Cooldown 题目链接:https://leetcode.com/problems... dp来解,要用两个dp array分别表示现在的操作是buy还是sell,优化空间用滚动数组,或者几个int public clas...

    sorra 评论0 收藏0
  • Best Time To Buy And Sell Stock 买卖股票最佳时机

    摘要:关键字,,算法,,动态规划,上关于主题的题目有四个这四个题目难度依次递增。其中第四个问题是寻求一个通解,在给定和最大买卖次数的情况下,求最大收益。首先大致的解题方向是动态规划,这个应该不难想到。之后就是怎么找到状态,怎么列状态转移方程。 关键字:leetcode,Best Time To Buy And Sell Stock,算法,algorithm,动态规划,dynamic prog...

    elliott_hu 评论0 收藏0
  • leetcode 121 Best Time to Buy and Sell Stock

    摘要:求可能的最大利润题目给了两个例子最大利润就是进价为,卖价为的时候,利润为在这个案例中,进价一直高于售价,所以无法成交,返回。主要注意一下,先买入才能卖出卖价一定要比买入价格高才能成交就可以了。 题目详情 Say you have an array for which the ith element is the price of a given stock on day i.If yo...

    QLQ 评论0 收藏0
  • Leetcode188. Best Time to Buy and Sell Stock IV

    摘要:题目要求有一个整数数组,每一位上的值代表那一天的股票价格。现在假设最多能够进行次交易,问最大的收入是多少思路和代码这里采用了动态编程的思想。 题目要求 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 t...

    pingink 评论0 收藏0

发表评论

0条评论

xcc3641

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<