# 0309. Best Time to Buy and Sell Stock with Cooldown ###### tags: `Leetcode` `Medium` `Dynamic Programming` Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ ## 思路 1. 今天在justBuy的状态上 说明可能今天继续保持了昨天的justBuy状态 也有可能从昨天的coolDown状态结束又买了一只新的股票 2. 今天在sell的状态上 说明有可能保持刚sell的状态 也有可能从昨天的justBuy状态结束卖了这只股票 3. 今天在coolDown状态上 昨天一定在sell的状态 要注意初始值怎么设定 ## Code ```java= class Solution { public int maxProfit(int[] prices) { int justBuy = Integer.MIN_VALUE; int coolDown = 0; int sell = 0; for(int i=0; i<prices.length; i++){ int justBuy_temp = justBuy; int sell_temp = sell; justBuy = Math.max(justBuy, coolDown-prices[i]); sell = Math.max(sell, justBuy_temp+prices[i]); coolDown = sell_temp; } return Math.max(coolDown, sell); } } ```