[309] LeetCode, the best time to buy and sell shares with the freezing of

Best Time to Buy and Sell Stock with Cooldown

Topic Level: Medium

Subject description:

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]

  Meaning of the questions: Given an array of integers, the first of which i -th element represents the first i -day stock price. Design an algorithm to calculate the maximum profit. In the following constraints, you can complete more transactions as much as possible, but there are two conditions: must sell before buying again, after selling the next day can not buy, have frozen on the day.


Problem-solving ideas (dynamic programming):

  He began trading stocks. . .

  Earlier we turn deal with the sale of shares of four topics: trade restrictions were once numerous transactions, transaction twice, trading k times. Here is a variant of numerous cases, adding a freezing period.

  The same problem-solving methods or dynamic programming, there is no limit to the number of transactions, so we define the state more convenient for dynamic programming, where every day is a stage, each stage of the decision-making is this: the decision to buy every day, sell or freeze, and every a day two states: the hands holding the stock, not the hands of stock (including the freezing of the case).

  It is possible to define two state variables:

  sell [i]: represents the maximum income is not in the hands of the stock after the end of the day i cases obtained.

  hold [i]: indicates the maximum gains in the case after the end of the day i still hold the hands of the stock.

  So, for these two states, still holding stocks if the end of the day, then there are two possibilities: (1) today just bought , then that must be the day before the freeze, and then move the hand one day no stock at the end of that hold[i]=sell[i-2]-prices[i]. (2) before you buy, and today nothing dry, did not buy nor sell, in other words, before the end of the day hands already have, namely:hold[i]=hold[i-1]

  If the end of the day when the stock no hands, then that there are two possibilities: (1) today just sold , that is to say before the end of the day when his hand is still holding the stock, so: sell[i]=hold[i-1]+prices[i]. (2) before you sell , today nothing dry , explained that before the end of the day, when the hands have no stock, so: sell[i]=sell[i-1].

  Taken together, we can get the recurrence relation:

    hold[i]=max(sell[i-2]-prices[i],hold[i-1])
    sell[i]=max(hold[i-1]+prices[i],sell[i-1])

  Another point to note is : the initial conditions , the first day of the time can not sell, can not freeze, the first day will buy , which in fact is greedy thought a reflection of the first day can buy must be bought, when dealing with an unlimited number of transactions has proved the correctness of this greedy thought so sell[0]=0, but hold[0]=-prices[0].

  Finally, the above analysis the following code:

class Solution {
    public int maxProfit(int[] prices) {
        if(prices==null || prices.length==0)
            return 0;
        int len=prices.length;
        int[] sell=new int[len];
        int[] hold=new int[len];
        sell[0]=0;
        hold[0]=-prices[0];
        for(int i=1;i<len;i++){
            sell[i]=Math.max(sell[i-1],hold[i-1]+prices[i]);
            hold[i]=Math.max(hold[i-1],(i>1?sell[i-2]:0)-prices[i]); //这里i-2注意防止越界
        }
        return sell[len-1];
    }
}

  Time complexity: O (n), the spatial complexity: O (2n)

Guess you like

Origin www.cnblogs.com/gzshan/p/11122371.html
Recommended