Problem

You are given an array prices where prices[i] is the price of a given stock on the iᵗʰ 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).

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

Example 1:

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

Example 2:

Input: prices = [1]
Output: 0

Constraints:

  • 1 <= prices.length <= 5000
  • 0 <= prices[i] <= 1000

Test Cases

1
2
class Solution:
def maxProfit(self, prices: List[int]) -> int:
solution_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
import pytest

from solution import Solution


@pytest.mark.parametrize('prices, expected', [
([1,2,3,0,2], 3),
([1], 0),
])
@pytest.mark.parametrize('sol', [Solution()])
class Test:
def test_solution(self, sol, prices, expected):
assert sol.maxProfit(prices) == expected

Thoughts

系列问题:

这次是在 122. Best Time to Buy and Sell Stock II(交易任意多次)的基础上,增加了 cooldown 一天的要求。

122. Best Time to Buy and Sell Stock II 中已经定义了 动态规划的状态及其转移函数,那么直接在转移函数上,把冷静期的影响加上去即可。更新后的转移函数为:

{holdi=max{holdi1,emptyi2prices[i]}emptyi=max{emptyi1,holdi1+prices[i]}\begin{cases} hold_i=\max\{hold_{i-1},empty_{i-2}-prices[i]\} \\ empty_i=\max\{empty_{i-1},hold_{i-1}+prices[i]\} \end{cases}

注意 hold(i) 的式子变了,因为在前一次卖出到今天买入,之间至少要隔开一天。

同样是 O(n) 时间,O(1) 空间。

Code

solution.py
1
2
3
4
5
6
7
8
9
class Solution:
def maxProfit(self, prices: list[int]) -> int:
max2 = lambda a, b: a if a >= b else b
hold = -prices[0]
empty = p_empty = 0
for price in prices:
p_empty, hold, empty = empty, max2(hold, p_empty - price), max2(empty, hold + price)

return empty