The best time to buy and sell stocks in LeetCode dynamic programming includes freezing period

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

topic

The timing of buying and selling stocks includes a freezing period

Given an array of integers prices, where   prices[i] ith represents the stock price on day i.

Design an algorithm to calculate the maximum profit. You can complete as many trades as possible (buying and selling a stock multiple times), subject to the following constraints:

After selling the stock, you cannot buy the stock the next day (ie, the freezing period is 1 day). Note: You cannot participate in multiple trades at the same time (you must sell the previous shares before buying again).

Example 1:

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

输入: prices = [1]
输出: 0
复制代码

hint:

1 <= prices.length <= 5000
0 <= prices[i] <= 1000
复制代码

answer

problem-solving analysis

Problem solving ideas

  1. From the meaning of the title, we can only buy one stock at most at the same time, and there is a restriction on the freezing period after selling the stock. On a certain day, we have three different states:

    • We currently hold a stock, and the corresponding "accumulated maximum return" is recorded as f0;
    • We currently do not hold any stocks and are in the freezing period, the corresponding "accumulated maximum return" is recorded as f1
    • We currently do not hold any stocks and are not in the freezing period. The corresponding "accumulated maximum return" is recorded as f2
  2. Then we analyze the three states.

    • If the current holding of the stock has the highest returnf0 = max(f1, f2 - price[i])
    • If it is difficult to be in the freezing period on that day, the current maximum profitf1 = f0 + price[i]
    • If the day is not in the freezing period, the maximum benefit of Dan lead isf2 = max(f1, f2)
  3. We only need to continuously calculate the three values ​​through the loop, and finally return the appropriate result.

Complexity Analysis

  • Time complexity: O(N)
  • Space complexity: O(1), we didn't create extra array to store.

problem solving code

The solution code is as follows (detailed comments in the code):

class Solution {
    public int maxProfit(int[] prices) {
        // 判断有效的参数
        if (prices == null || prices.length == 0) {
            return 0;
        }
        // 获取数组长读
        int n = prices.length;
        // f0, f1, f2 默认值分别是 -prices[0], 0, 0
        // 因为第一天是负收益
        int f0 = -prices[0], f1 = 0, f2 = 0;
        for (int i = 1; i < n; i++) {
            int newf0 = Math.max(f0, f2 - prices[i]);
            int newf1 = f0 + prices[i];
            int newf2 = Math.max(f1, f2);

            f0 = newf0;
            f1 = newf1;
            f2 = newf2;
        }
        return Math.max(f1, f2);
    }
}
复制代码

Feedback results after submission:

image.png

Reference Information

Guess you like

Origin juejin.im/post/7080143145358491678