Best Time to Buy and Sell Stock with Cooldown

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

# Solution

def maxProfit(self, prices: List[int]) -> int:
    n = len(prices)
    dp = [[0 for _ in range(2)] for _ in range(n)]
    dp[0][0] = 0
    dp[0][1] = -prices[0]
    for i in range(1, n):
        dp[i][0] = max(dp[i-1][0], dp[i-1][1]+prices[i])
        dp[i][1] = max(dp[i-1][1], dp[i-2][0]-prices[i])
    return dp[n-1][0]
1
2
3
4
5
6
7
8
9