Leetcode - 309. 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:

Input: [1,2,3,0,2]
Output: 3 
Explanation: transactions = [buy, sell, cooldown, buy, sell]
----------------------------
 
Solution:

The idea is as follows:

 

First, think about what we can do on day i? You either have one stock or you don't on day i. For each case, you have two options, making a total of four possible actions on day i:

 

  1. you have 1 stock and you sell it
  2. you have 1 stock and you do nothing
  3. you have 0 stock and you buy stock i
  4. you have 0 stock and you do nothing

 

As you can imagine, these four actions are correlated between day i-1 and day i. For example, if you take action 1 on day i, you then have either taken action 2 or 3 on day i-1 but not 1 or 4. In precise, two consecutive days are related as follows:

 

  1. if you take action 1 on day i ==> you have either taken action 2 or 3 on day i-1
  2. if you take action 2 on day i ==> you have either taken action 2 or 3 on day i-1
  3. if you take action 3 on day i ==> you must have taken action 4 on day i-1 (you can not sell on day i-1 due to cool down)
  4. if you take action 4 on day i ==> you have either taken action 1 or 4 on day i-1

 

Now you want to maximize your total profit, but you don't know what action to take on day i such that you get the total maximum profit, so you try all 4 actions on every day. Suppose you take action 1 on day i, since there are two possible actions on day i-1, namely actions 2 and 3, you would definitely choose the one that makes your profit on day i more. Same thing for actions 2 and 4. So we now have an iterative algorithm.

 

Before coding, one detail to emphasize is that the initial value on day 0 is important. You basically cannot take action 1, so the corresponding profits should be 0. You cannot take action 2 in practice, but you cannot set up the profit to 0, because that means you don't have a stock to sell on day 1. Therefore, the initial profit should be negative value of the first stock. You can also think of it as you buy the stock on day -1 and do nothing on day 0.

复制代码
 1 public int maxProfit(int[] prices) {
 2 
 3     if (prices.length < 1) return 0;
 4     
 5     int has0_buy = -prices[0];
 6     int has0_doNothing = 0;
 7     int has1_sell = 0;
 8     int has1_doNothing = -prices[0];
 9     
10     for (int i = 1; i < prices.length; i++) {
11         int l1 = has0_buy;
12         int l2 = has0_doNothing;
13         int l3 = has1_sell;
14         int l4 = has1_doNothing;
15         
16         has0_buy = l2 + -prices[i];
17         has0_doNothing = Math.max(l3, l2);
18         has1_sell = Math.max(l1,l4) + prices[i];
19         has1_doNothing = Math.max(l1, l4);
20     }
21 
22     return Math.max(has0_doNothing, has1_sell);
23 }
复制代码

 

 

posted on   frank_cui  阅读(123)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示