[Leetcode Daily Notes] 123. The Best Time to Buy and Sell Stocks III (Python)

topic

Given an array, its i-th element is the price of a given stock on the i-th day.

Design an algorithm to calculate the maximum profit you can get. You can complete up to two transactions.

Note: You cannot participate in multiple transactions at the same time (you must sell the previous stocks before buying again).

Example 1:

Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on the 4th day (stock price = 0), and on the 6th day (stock price =
3) When it is time to sell, this exchange can make a profit = 3-0 = 3.
Then, buy on the 7th day (stock price = 1) and sell on the 8th day (stock price = 4). This exchange can make a profit = 4-1 = 3.

Example 2:

Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (stock price =
1), sell on day 5 (stock price = 5), this The exchange can make a profit = 5-1 = 4.
Note that you cannot buy stocks one after another on the first day and the second day, and then sell them later.
Because this is involved in multiple transactions at the same time, you must sell the previous stocks before buying again.

Example 3:

Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is completed, so the maximum profit is 0.

Problem-solving ideas

Dynamic programming

State definition

Through the topic, we can find that the variables involved are the number of days, the number of transactions, and the status of buying and selling. Therefore, a three-dimensional array dp[i][j][k] can be defined to represent the cumulative maximum profit of j transactions on the i-th day. , K indicates whether this operation is to buy or sell. For example, dp[1][1][0] represents the cumulative maximum profit after selling once on the first day, so there are five states on the i-th day: dp[i][0][0], dp [i][0][1], dp[i][1][0], dp[i][1][1], dp[i][2][0]. dp[i][0][0]: Corresponding to the initial state, 0 transactions are sold on the i day. Since there are no transactions, how can I sell, so it can only be 0. The two dp[i][0][1] and dp[i][1][0] are a pair, which corresponds to the first purchase and the first sale in the above figure. The two dp[i][1][1] and dp[i][2][0] are also a pair, which corresponds to the second buying and second selling in the above figure.
Then think about it, and find that there are only five states every day, then you can simplify the state definition and express it directly in the following way

  • dp[i][0] Initialization state

  • dp[i][1] The first purchase

  • dp[i][2] first sell

  • dp[i][3] second buy

  • dp[i][4] is sold for the second time,
    combined with the following state transition equation, it can be found that the profit on the i-th day is only related to the i-1th day, that is to say, the profit on the i-th day is calculated, The profit of the i-1 day and the previous day is not needed, so you can use a one-dimensional array for rolling update, only the data of the i-1 day and the i day, that is, the previous day and the current profit.

  • dp0: Initialization state

  • dp1: first purchase

  • dp2: first sell

  • dp3: second purchase

  • dp4: second sale

State transition equation

The state transition equation of the three-dimensional array:

The first purchase: Converted from the initial state, or remained unchanged after the first purchase dp[i][0][1] =
max(dp[i-1][0][1],dp [i-1][0][0]-prices[i])

First sale: Converted from the first purchase, or stayed still after the first sale dp[i][1][0] =
max(dp[i-1][1][0 ],dp[i-1][0][1]+prices[i])
Second purchase: Converted from the first sale, or remain unchanged after the second purchase dp[i] [1][1] =
max(dp[i-1][1][1],dp[i-1][1][0]-prices[i])

Second sale: Converted from the second purchase, or remain unchanged after the second sale dp[i][2][0] =
max(dp[i-1][2][0 ],dp[i-1][1][1]+prices[i])

The state transition equation of a two-dimensional array:

The first purchase: Converted from the initial state, or remained unchanged after the first purchase dp[i][1] =
max(dp[i-1][1],dp[i-1][ 0]-prices[i])
The first sale: Converted from the first purchase, or kept still after the first sale dp[i][2] = max(dp[i-1] [2],dp[i-1][1]+prices[i])

Second purchase: Convert from the first sale, or remain unchanged after the second purchase dp[i][3] =
max(dp[i-1][3],dp[i- 1][2]-prices[i])

Second sale: Converted from the second purchase, or remain unchanged after the second sale dp[i][4] =
max(dp[i-1][4],dp[i- 1][3]+prices[i])

The state transition equation of a one-dimensional array:

First purchase: Converted from the initial state, or kept still after the first purchase dp1 = max(dp1,dp0-prices[i])
First sale: Converted from the first purchase Come, or stay still after the first sale dp2 = max(dp2,dp1+prices[i])

Second purchase: Converted from the first sale, or remain unchanged after the second purchase dp3= max(dp3,dp2-prices[i])

Second sale: Converted from the second purchase, or remain unchanged after the second sale dp4 = max(dp4,dp3+prices[i])

Code

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        dp0 = 0
        dp1 = -prices[0]
        dp2 = 0
        dp3 = -prices[0]
        dp4 = 0
        for i in range(1,len(prices)):
            dp1 = max(dp1,dp0-prices[i])
            dp2 = max(dp2,dp1+prices[i])
            dp3 = max(dp3,dp2-prices[i])
            dp4 = max(dp4,dp3+prices[i])
        return max(dp0,dp1,dp2,dp3,dp4)

Guess you like

Origin blog.csdn.net/qq_36477513/article/details/111831663