Day51|leetcode 309. The best time to buy and sell stocks includes a freezing period, 714. The best time to buy and sell stocks includes handling fees

leetcode 309. The best time to buy and sell stocks includes a freezing period

Topic link: 309. The best time to buy and sell stocks includes a freezing period - LeetCode

Video link: Dynamic programming to determine the best time, this time there is a freezing period! | LeetCode: 309. The best time to buy and sell stocks includes a freezing period_哔哩哔哩_bilibili

topic overview

Given an array of integers, where the i-th represents the stock price on the i-th day.

Design an algorithm to calculate the maximum profit. You can complete as many transactions as possible (buy and sell a stock multiple times) subject to the following constraints:

  • After selling the stock, you cannot buy the stock the next day (that is, the freezing period is 1 day).

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

 

Example 1:

Input: prices = [1,2,3,0,2]
 Output: 3 
 Explanation: The corresponding transaction status is: [buy, sell, freeze period, buy, sell]

Example 2:

Input: prices = [1]
 Output: 0

train of thought

It's still a five-part series

1. Determine the meaning of the dp array

dp[i][j]: The status on the i-th day is j, and the most remaining cash is dp[i][j].

If we follow the thinking of the previous questions, then stocks will be divided into two states: holding stocks and not holding stocks . However, because there is a freezing period in this question, the state of not holding stocks will be further subdivided .

(1) dp[i][0]: stock holding status (bought today or before)

No stock status:

(2) dp[i][1]: Keep selling stocks (sold stocks in the previous two days, passed the one-day freezing period or sold stocks in the previous day)

(3) dp[i][2]: sell stocks today

(4)dp[i][3]: freezing period (only one day)

2. Determine the recursive formula

(1) dp[i][0]: stock holding status

1) Bought the day before: dp[i - 1][0]

2) I bought it today, but the previous day was a freezing period: dp[i - 1][3] - prices[i]

3) I bought it today, but I kept selling the stock the day before: dp[i - 1][1] - prices[i]

所以dp[i][0] = max(dp[i - 1][0], dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i])

(2) dp[i][1]: Keep selling stocks

1) Keep selling stocks the day before: dp[i - 1][1]

2) The previous day is the freezing period: dp[i - 1][3]

所以dp[i][1] = max(dp[i - 1][1], dp[i - 1][3])

(3) dp[i][2]: sell stocks today

1) Sell today: dp[i][2] = dp[i - 1][0] + prices[i]

(4) dp[i][3]: freezing period

1) The stock was sold yesterday: dp[i][3] = dp[i - 1][2]

3. Array initialization

dp[0][0] = -prices[0] (the stock must be bought on the same day)

dp[0][1]=0

dp[0][2]=0

dp[0][3]=0

4. Determine the traversal order

front to back

5. Print the array (take 1, 2, 3, 0, 2 as an example)

Code

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if(len == 0) return 0;
        vector<vector<int>> dp(len,vector<int>(4,0));
        dp[0][0] -= prices[0];
        for(int i = 1;i < len;i++) {
            dp[i][0] = max(dp[i - 1][0],max(dp[i - 1][3] - prices[i],dp[i - 1][1] - prices[i]));
            dp[i][1] = max(dp[i - 1][1],dp[i - 1][3]);
            dp[i][2] = dp[i - 1][0] + prices[i];
            dp[i][3] = dp[i - 1][2];
        }
        return max(dp[len - 1][3],max(dp[len - 1][1],dp[len - 1][2]));
    }
};

Leetcode 714. The best time to buy and sell stocks includes handling fees

Topic link: 714. The best time to buy and sell stocks includes handling fees - LeetCode

Video link: Dynamic programming to determine the best time, this time including handling fees! | LeetCode: 714. The best time to buy and sell stocks includes handling fees_哔哩哔哩_bilibili

topic overview

Given an integer array prices, the i-th element represents the stock price on the i-th day; the non-negative integer fee represents the handling fee for trading stocks.

You can complete transactions an unlimited number of times, but you need to pay a transaction fee for each transaction. If you have already bought a stock, you cannot continue to buy another stock until you sell it.

Returns the maximum value of profit.

Note: A transaction here refers to the entire process of buying, holding and selling stocks, and you only need to pay a handling fee for each transaction.

Example 1:

   Input: prices = [1, 3, 2, 8, 4, 9], fee = 2

   output: 8

Explanation: The maximum profit that can be achieved:

   buy here prices[0] = 1

   sell here prices[3] = 8

   buy here prices[4] = 4

   sell here prices[5] = 9

   Total profit: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

train of thought

This question is almost the same as Question 122 (The best time to buy and sell stocks||), the only difference is that this question needs to subtract the handling fee.

Code

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int len = prices.size();
        if(len == 0) return 0;
        vector<vector<int>> dp(len,vector<int>(2,0));
        dp[0][0] -= prices[0];
        for(int i = 1;i < prices.size();i++) {
            dp[i][0] = max(dp[i - 1][0],dp[i - 1][1] - prices[i]);
            dp[i][1] = max(dp[i - 1][1],dp[i - 1][0] + prices[i] - fee);
        }
        return max(dp[len - 1][0],dp[len - 1][1]);

    }
};

おすすめ

転載: blog.csdn.net/m0_74583479/article/details/132589867