LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
考点 | 难度 |
---|---|
DP | Medium |
题目
You are given an array prices where prices[i]
is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
思路
bottom-up dynamic programming
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/761981/PythonGoJavaJSC%2B%2B-O(n)-by-DP-and-state-machine.-w-Visualization
答案
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# initialization
cool_down, sell, hold = 0, 0, -float('inf')
for stock_price_of_Day_i in prices:
prev_cool_down, prev_sell, prev_hold = cool_down, sell, hold
# Max profit of cooldown on Day i comes from either cool down of Day_i-1, or sell out of Day_i-1 and today Day_i is cooling day
cool_down = max(prev_cool_down, prev_sell)
# Max profit of sell on Day_i comes from hold of Day_i-1 and sell on Day_i
sell = prev_hold + stock_price_of_Day_i
# Max profit of hold on Day_i comes from either hold of Day_i-1, or cool down on Day_i-1 and buy on Day_i
hold = max(prev_hold, prev_cool_down - stock_price_of_Day_i)
# The action of final trading day must be either sell or cool down
return max(sell, cool_down)