zoukankan      html  css  js  c++  java
  • LeetCode_Best Time to Buy and Sell Stock III

    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 at most two transactions.
    
    Note:
    You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
    
    分析: First consider the case that when we are only allowed to make one transaction. we can handle this easily with DP. If we move forward, any new price we meet will only affect our history result by two ways:
    
    will it be so low that it beats our previous lowest price?
    will it be so high that we should instead sell on this time to gain a higher profit (than the history record)? Similarly, we can move backward with the highest price and profit in record. Either way would take O(n) time.
    Now consider the two transaction case. Since there will be no overlaps, we are actually dividing the whole time into two intervals.
    
    We want to maximize the profit in each of them so the same method above will apply here. We are actually trying to break the day at each time instance, by adding the potential max profit before and after it together. By recording history and future for each time point, we can again do this within O(n) time.
    
    摘自 : http://discuss.leetcode.com/questions/287/best-time-to-buy-and-sell-stock-iii?sort=votes&page=1
    

      

    class Solution {
    public:
    //III
        int maxProfit(vector<int> &prices) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            int len = prices.size();
            if(len < 2) return 0;
            
            int minheight, maxheight;
            vector<int> history(len, 0);
            vector<int> future(len, 0);
            
            minheight = prices[0];
            history[0] = 0;
            for( int i = 1; i< len; ++i){
                if(minheight > prices[i])
                    minheight = prices[i];
        
                history[i] = history[i-1] > prices[i]- minheight ? history[i-1]
                                                                 : prices[i] - minheight;        
            }
            
            
            int res = 0;
            maxheight = prices[len-1];
            future[len-1] = 0;
            for( int i = len -2; i>=0 ; --i){
                if(maxheight < prices[i])
                    maxheight = prices[i];
                    
                future[i] = future[i+1] > maxheight - prices[i] ? future[i+1]
                                                                : maxheight  - prices[i] ;
            
                res = res > future[i] + history[i] ? res : future[i]+ history[i]; 
            }
            
            return res;
        }
    };
  • 相关阅读:
    js 解压缩编码列表
    js 拥有最多糖果
    js 所有奇数长度子数组的和
    js和jquery中有关透明度操作的问题
    python pandas初体验
    NumPy基础及取值操作
    python之pandas简介
    Study Plan The FortyThird day
    Study Plan The ThirtyNine Day
    Study Plan The FortySeventh Day
  • 原文地址:https://www.cnblogs.com/graph/p/3319460.html
Copyright © 2011-2022 走看看