码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode121/122/123 Best Time to Buy and Sell Stock<股票> I/II/III----DP+Greedy**

时间:2015-04-11 20:52:40      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:dp   greedy   leetcode   array   

一:LeetCode 121 Best Time to Buy and Sell Stock

题目:

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

分析:此题就是选择买入卖出股票的最大收益,对于第i天卖出的最大收益即为第i天的股市价格减去[0,i-1]天内的最小股市价格,当第i天的股市价格比漆面最低股市价格还低,则更新最低股市价格。然后取最大的股市收益,为DP问题。用profit[i]表示第i天的收益,则minBuyPrice = min(minBuyPrice, prices[i]),并且profit[i] = prices[i]-minBuyPrice.   然后取profit中的最大值。

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if(n == 0) return 0;
        int maxPro = 0;
        int minBuyPrice = prices[0];
        for(int i = 1; i < n; i++){
            minBuyPrice = min(minBuyPrice, prices[i]);   // 用于记录当前天买进的最小值 minBuyPrice[i+1] = min(minBuyPrice[i], nums[i])
            if(maxPro < (prices[i]- minBuyPrice)){     // 全局最大利益是否小于当天的利益 
                maxPro = prices[i]- minBuyPrice;
            }
        }
        return maxPro;
        
    }
};

二:LeetCode 122 Best Time to Buy and Sell Stock II

题目:

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

链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

分析:此题是上题的变形,买入卖出的次数没有限制,但是第二次买入必须在第一次卖出的时间节点之后,,此时存在一个局部最优,即 2 4 5 3 6 8,此时8-2一定小于5-2+8-3,,因此就有取数组每次递增的收益即为局部最优,然后所有的局部最优加起来就是全局最优

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if(n == 0) return 0;
        int minBuyPrice = prices[0];
        int sumResult = 0;
        /*for(int i = 0; i < n; i++){
            if(i+1 < n && prices[i] >= prices[i+1]){     // 局部最优在于当prices[i] >= prices[i+1] 
                sumResult += prices[i]-minBuyPrice;    //既可用prices[i]-minBuyPrices了找到局部最优了--全部加起来就是全局最优解了
                minBuyPrice = prices[i+1];
            }else{
                if(i+1 == n) sumResult += prices[i]-minBuyPrice;
            }
        }*/
        for(int i = 1; i < n; i++){
            if(prices[i] < prices[i-1]){           // 在i处有一个局部最优,所有的局部最优和就是全局最优
                sumResult += prices[i-1]- minBuyPrice;
                minBuyPrice = prices[i];
            }
        }
        sumResult += prices[n-1]- minBuyPrice;
        return sumResult;
    }
};
后来在discuss中看到一段更牛逼的代码:只要十行:

class Solution {
public:
    int maxProfit(vector<int> &a) {
        int profit = 0;
        for (int i = 1; i < a.size(); ++i) {
            if (a[i] > a[i-1]) {
                profit += a[i]-a[i-1];
            }
        }
        return profit;
    }
};
三:LeetCode 123 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).

链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

分析:此题亦即变形,也就是求交易次数最多为两次。当然有两种方法,第一种暴力,对于每个i,我们求[0,i]与[i,n-1]两次收益,然后求和,遍历i,可以取其中的最大值,需要O(N^2)的时间。第二种方法是动态规划,用两个数组,第一个数组f1[i]用来表示在[0,i]内进行买入卖出的最大收益,用f2[i]表示在[i,n-1]内进行买入卖出的最大收益。然后最大收益即为max(f1[i]+f2[i]),如何求f1[i]和f2[i],,第一题的方法已经给出了。

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if(n <= 1) return 0;
        vector<int> f1(n);  // 表示在[0,i]内进行买入卖出所能获得的最大profit
        vector<int> f2(n);  // 表示在[i, n-1]内进行买入卖出所能获得的最大profit  结果就为max(f1[i]+f2[i])
        
        int minPrice = prices[0];
        
        for(int i = 1; i < n; i++){
            minPrice = min(minPrice, prices[i]);
            f1[i] = max(f1[i-1], prices[i]- minPrice);   
        }
        
        int maxPrice = prices[n-1];
        for(int i = n-2; i >=0; i--){     // 这里要从后往前遍历
            maxPrice = max(maxPrice, prices[i]);
            f2[i] = max(f2[i+1], maxPrice - prices[i]);
        }
        int maxResult = 0;
        for(int i = 0; i < n; i++)
            maxResult = max(maxResult, f1[i]+f2[i]);
        return maxResult;
    }
        
};





LeetCode121/122/123 Best Time to Buy and Sell Stock<股票> I/II/III----DP+Greedy**

标签:dp   greedy   leetcode   array   

原文地址:http://blog.csdn.net/lu597203933/article/details/44998499

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!