LeetCode | 0309. Best Time to Buy and Sell Stock with Cooldown best time to buy and sell shares with the freezing of [Python]

LeetCode 0309. Best Time to Buy and Sell Stock with Cooldown best time to buy and sell shares with the freezing of [Medium] [] [Dynamic Programming Python]

Problem

LeetCode

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]

problem

Power button

Given an array of integers, which represents the i-th element of the i-day stock price.

Design an algorithm to calculate the maximum profit. In the following constraints, you can close more deals as possible (buy and sell a stock):

  • You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).
  • After selling the stock, you can not buy the stock the next day (ie freeze to one day).

Example:

输入: [1,2,3,0,2]
输出: 3 
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]

Thinking

Dynamic Programming

找到状态方程

dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
解释:昨天没有股票,昨天有股票今天卖出

dp[i][k][1] = max(dp[i-1][k][1], dp[i- 2][k][0] - prices[i])
解释:昨天有股票,前天刚卖出股票昨天冷冻期今天买入
第 i 天选择 buy 的时候,要从 i-2 的状态转移,而不是 i-1 。

base case:
dp[-1][k][0] = dp[i][k][0] = 0
dp[-1][k][1] = dp[i][k][1] = -inf

k = +inf
因为 k 为正无穷,那么可以把 k 和 k-1 看成是一样的。
buy+sell = 一次完整的交易,这里把 sell 看成一次交易,所以第一行是 k-1。
dp[i][k][0] = max(dp[i-1][k][0], dp[i-1][k-1][1] + prices[i])
            = max(dp[i-1][k][0], dp[i-1][k][1] + prices[i])
dp[i][k][1] = max(dp[i-1][k][1], dp[i-2][k][0] - prices[i])

所以 k 对状态转移没有影响:
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])

i = 0 时,dp[i-1] 不合法。
dp[0][0] = max(dp[-1][0], dp[-1][1] + prices[i])
         = max(0, -infinity + prices[i])
         = 0
dp[0][1] = max(dp[-1][1], dp[-1][0] - prices[i])
         = max(-infinity, 0 - prices[i]) 
         = -prices[i]

Space complexity: O (. 1)

Python3 Code
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        dp_i_0 = 0
        dp_i_1 = float('-inf')  # 负无穷
        dp_pre_0 = 0  # 表示 dp[i-2][0]
        for i in range(len(prices)):
            temp = dp_i_0
            # 昨天没有股票,昨天有股票今天卖出
            dp_i_0 = max(dp_i_0, dp_i_1 + prices[i])  # dp_i_0 和 dp_i_1 可以看成是变量,存储的都是上一次即昨天的值
            # 昨天有股票,前天刚卖出股票昨天冷冻期今天买入
            dp_i_1 = max(dp_i_1, dp_pre_0 - prices[i])
            dp_pre_0 = temp

        return dp_i_0

Code address

GitHub link

reference

One way the group off six stock issue

Guess you like

Origin www.cnblogs.com/wonz/p/12465266.html
Recommended