Best Time to Buy and Sell Stock系列分析

_yannnnn發表於2018-06-21
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int size= prices.size();
        int profit = 0;
        for(int i=0;i<size;i++){
            for(int j=i+1;j<size;j++){
                if((prices[j] - prices[i])>profit){
                    cout<<i<<" "<<j<<endl;
                    profit = prices[j] - prices[i];
                }
            }
        }
        return profit;
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int size= prices.size();
        int profit = 0;
        for(int i=1;i<size;i++){
            if(prices[i-1]<prices[i]){
                cout<< prices[i]<<" "<<prices[i-1]<<endl;
                profit += (prices[i]-prices[i-1]);
            }
        }
        return profit;
    }
};
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        const int size = prices.size();
        if(size<2){
            return 0;
        }
        int profit1[size+1], profit2[size+1];
        int minprice = prices[0];
        int maxprice = prices[size-1];
        profit1[0] = 0;
        for(int i=1;i<size;i++){
            profit1[i] = max(profit1[i-1], prices[i] -  minprice);
            if(minprice > prices[i]){
                minprice = prices[i];
            }
        }
        profit2[size-1] = 0;
        for(int i=size-2;i>=0;i--){
            profit2[i] = max(profit2[i+1], maxprice - prices[i]);
            if(maxprice < prices[i]){
                maxprice = prices[i];
            }
        }
        int global = 0;
        for(int i=0;i<size;i++){
            global = max(global, profit1[i]+profit2[i]);
        }

        return global;
    }


};
  • Best Time to Buy and Sell Stock IV
    來自 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/discuss/
    題目描述:明確規定最多隻能進行k次交易
    將問題拆分成k個子問題,第0天到第cc天,第cc+1天到最後一天
    用global和local來更新,考慮到第j天的交易可能跟第j-1天重複了 本來是1次交易但記錄成兩次
    另外,如果k很大的時候,退化成第二種問題。注意,當k很大的時候,我們開陣列的時候要小心,不然會爆掉,然後Runtime error
    local[j] = max(global[j-1]+max(diff,0), local[j]+diff);
    global[j] = max(local[j], global[j]);
class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        int size = prices.size();
        if(size<2){
            return 0;
        }
        if(k>=size){
            return solveMaxProfit(prices);
        }
        int local[k+1]={0};//k太大的時候 建立陣列的時候爆掉了···
        int global[k+1]={0};
        for(int i=0;i<size-1;i++){
            int diff = prices[i+1]-prices[i];
            for(int j=k;j>=1;j--){
                local[j] = max(global[j-1]+max(diff,0), local[j]+diff);
                global[j] = max(local[j], global[j]);
            }
        }
        return global[k];

    }
    int solveMaxProfit(vector<int>& prices) {
        int size = prices.size();
        int result = 0;
        for(int i=1;i<size;i++){
            if(prices[i] - prices[i-1]>0){
                result += prices[i]-prices[i-1];
            }
        }
        return result;
    }
};

相關文章