[] Leetcode stock trading Series summary

Series stock trading summary

Series title stock trading in the interview is quite classic, where this series do some simple summary.

1. Only once allowed trading

Assuming that the stock price sequence(3, 5, 7, 3, 8, 1)

Dynamic Programming. Throughout the process there are three kinds of behavior select, buy / sell / no-op.

With \ (dp [i] \) denotes \ (i \) behavior is the biggest day of gains to "sell" can get. Obviously, we sell a fixed period of time, long time before this point in time, select the price to buy the stock than the minimum of time, you can determine \ (dp [i] \) values.

So we can traverse the price series, cur_minconstantly updated to stock the lowest price recorded before the current point in time, so the \ (dp [i] = max (0, Prices [i] -cur \ of _min) \) , and finally all dp [i] the maximum value is only once the sale to get the maximum profit. May find \ (dp [i] \) with a variable maximum recorded simultaneously.

Since \ (dp [i] \) used only once, so there is no need to open a dedicated array to store.

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // 只允许最多一次交易 求最大收益
        // 记录当前最小  cur_min dp[i]表示在第i天卖出的最大收益
        if(prices.size()<=1) return 0;
        int cur_min = prices[0], res = 0;
        for(int i=1; i<prices.size(); i++){
            res = max(prices[i]-cur_min, res);// dp[i] = prices[i] - cur_min;
            cur_min = min(cur_min, prices[i]);
        }
        return res;
    }
};

2. Without limiting the number of trading

Assuming that the stock price sequence (1, 2, 3, 4, 5). In fact, this is a special case, we can buy at day 0, and then sold in the last day. At this time, the maximum benefit is 4.

We can also buy at day 0, the first day of selling. The first day to buy, sell the next day. . . This result is the same as above, the maximum benefit is four.

So, for more conventional circumstances, for example (7, 6, 3, 4, 5), in the first and second day we can not buy, because, after not find a greater price than the current number. In addition, you can buy when 3, 4 when sold, then bought another 4, 5 sell. Finally, the maximum benefit is 2.

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // 不限制股票的买卖次数
        // 只要是遇到比当前价格更高的就卖掉
        if(prices.size() <=1) return 0;
        int buy = INT_MAX, profit = 0;
        for(int i=0; i<prices.size(); i++){
            if(prices[i]<=buy) buy = prices[i];
            else{
                profit += prices[i]-buy;
                buy = prices[i];
            }
        }
        return profit;
    }
};

The above code can be simplified to:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() <=1) return 0;
        int buy = INT_MAX, profit = 0;
        for(int i=0; i<prices.size(); i++){
            if(prices[i] > buy) profit += prices[i] - buy;// 只有当当前价格大于假设买入的价格时,才进行卖出
            buy = prices[i];// 每次都在当前进行“假设”买入
        }
        return profit;
    }
};

3. The sale of a maximum of two times

\ (buy1 \) represents the first \ (i \) the maximum income that can be obtained when you first buy day
\ (buy2 \) represents the first \ (i \) be available when buying second day the maximum income
\ (sell1 \) represents the first \ (i \) be available when the first day of selling the maximum benefit
\ (sell2 \) represents the first \ (i \) may be sold during a second day the largest gains

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // 最多只能买卖2次 求最大收益
        // buy1 sell1 buy2 sell2 分别表示当前天如果是第一次买/卖、第二次买/卖时的最大收益
        int buy1 = INT_MIN, buy2 = INT_MIN, sell1 = 0, sell2 = 0;
        for(int price:prices){
            sell2 = max(sell2, buy2+price);
            buy2 = max(buy2, sell1-price);
            sell1 = max(sell1, buy1+price);
            buy1 = max(buy1, -price);
        }
        return sell2;
    }
};

4. There are no restrictions on the sale of frozen times

\ (buy [i] \) denotes \ (i \) days prior to the last behavior is buy, maximum benefit

\ (sell [i] \) denotes \ (i \) days prior to the last behavior is sell, maximum benefit

\ (rest [i] \) denotes \ (i \) days prior to the last behavior is freezing rest, maximum benefit

\ (Buy [i] = max (Buy [. 1-i], REST [. 1-i] - Prices [i]) \) , max (frozen on day i, the i-th day is a sell)

\ (Sell [i] = max (Sell [. 1-i], Buy [. 1-i] + Prices [i]) \) , max (frozen on day i, the i-th day is a buy)

\(rest[i] = max(rest[i-1], buy[i-1], sell[i-1])\)

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // 有冷冻期 不限制买卖次数 但是卖完股票后有一天的冷冻期才能再接着买
        int n = prices.size();
        if(n<=1) return 0;
        vector<int> buy(n, INT_MIN), sell(n, INT_MIN), rest(n, INT_MIN);
        buy[0] = -prices[0];
        sell[0] = 0;
        rest[0] = 0;
        for(int i=1; i<n; i++){
            buy[i] = max(buy[i-1], rest[i-1]-prices[i]);
            sell[i] = max(sell[i-1], buy[i-1]+prices[i]);
            rest[i] = max(rest[i-1], max(buy[i-1], sell[i-1]));
        }
        return max(buy[n-1], max(sell[n-1], rest[n-1]));
    }
};

Guess you like

Origin www.cnblogs.com/Elaine-DWL/p/11202859.html