Best Time to Buy and Sell Stock1,2,3,4

 

     

找到最低值和最高值

int maxProfit(vector<int>& prices) {
        if(prices.size()<2)return 0;
        int profit=0;
        int cur_min=prices[0];
        for(int i=1;i<prices.size();i++)
        {
            profit=max(profit,prices[i]-cur_min);//记录最大利润
            cur_min=min(cur_min,prices[i]);//保留购买最小值
        }
        return profit;
    }

 2、

      计算差分序列,大于0加入

      

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

 

 

 

3、

      把交易分成两次,分别完成,最后将利润相加求最大。

      

public int maxProfit(int[] prices) {
        if (prices.length < 2) return 0;
        
        int n = prices.length;
        int[] preProfit = new int[n];
        int[] postProfit = new int[n];
        
        int curMin = prices[0];
        for (int i = 1; i < n; i++) {
            curMin = Math.min(curMin, prices[i]);
            preProfit[i] = Math.max(preProfit[i - 1], prices[i] - curMin);
        }
        
        int curMax = prices[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            curMax = Math.max(curMax, prices[i]);
            postProfit[i] = Math.max(postProfit[i + 1], curMax - prices[i]);
        }
        
        int maxProfit = 0;
        for (int i = 0; i < n; i++) {
            maxProfit = Math.max(maxProfit, preProfit[i] + postProfit[i]);
        }
        
        return  maxProfit;
    }

 

 

 

4、

  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 at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

  

public int maxProfit(int k, int[] prices) {
        if (prices.length < 2) return 0;
        
        int days = prices.length;
        if (k >= days) return maxProfit2(prices);
        
        int[][] local = new int[days][k + 1];
        int[][] global = new int[days][k + 1];
        
        for (int i = 1; i < days ; i++) {
            int diff = prices[i] - prices[i - 1];
            
            for (int j = 1; j <= k; j++) {
                local[i][j] = Math.max(global[i - 1][j - 1], local[i - 1][j] + diff);
                global[i][j] = Math.max(global[i - 1][j], local[i][j]);
             }
        }
        
        return global[days - 1][k];
    }
    
    
    public int maxProfit2(int[] prices) {
        int maxProfit = 0;
        
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1]) {
                maxProfit += prices[i] - prices[i - 1];
            }
        }
        
        return maxProfit;
    }

 

 

 

  参考:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html

  可以交易k次,没看懂,感觉自己好笨。。。

posted @ 2016-12-29 17:07  牧马人夏峥  阅读(179)  评论(0编辑  收藏  举报