您的位置:首页 > 其它

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

2015-04-11 20:30 525 查看
一: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;
    }
        
};


四:leetcode 188 Best
Time to Buy and Sell Stock IV *****

题目:

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).

分析:此题为较难的动态规划问题。首先要第一步就是要转化成动态规划问题, 其次是要写出递归式。我们用local[i][j]表示第i天最多交易j次并且最后一次卖出在第j天所获得的最大利润;global[i][j]用来表示第i天最多交易j次所获得的最大利润。
那么local[i][j]的递归式为:

local[i][j] = max(global[i-1][j-1]+ max(diff, 0), local[i-1][j]+diff).

其中diff = prices[i] - prices[i-1]; global[i-1][j-1]表示第i-1天最多进行j-1次交易获得的最大利润,如果diff大于0,那么那么就加上今天获得利润,diff<0,那么最后一次就可以看做是当天买入并卖出。local[i-1][j]表示第i-1天最多进行j次交易获得的利润,加上diff就等于local[i][j]。 取两者最大值。

global[i][j]的递归式为:

global[i][j] = max(global[i-1][j], local[i][j]);

也就是取第i-1天最多进行j次交易和第i天交易并且最多交易j次且最后一次交易在第i天的利润的最大值。

代码:

class Solution {
public:

    int maxProfit(int k, vector<int>& prices) {
        int n = prices.size();
        if(k >= n/2) return maxProfit2(prices);
        vector<int> lp(k+1);
        vector<int> gp(k+1);
        for(int i = 1; i < n; i++){
            int diff = prices[i]- prices[i-1];
            for(int j = k; j>0; j--){
                lp[j] = max(gp[j-1]+max(diff, 0), lp[j]+diff);
                gp[j] = max(gp[j], lp[j]);
            }
        }
        return gp[k];
        
    }
    int maxProfit2(vector<int> &prices){
        int ans = 0;
        for(int i = 1; i < prices.size(); i++){
            ans += max(prices[i]-prices[i-1], 0);
        }
        return ans;
    }
};


此题所需要的时间复杂度为O(NK), 空间复杂度为O(K)。另外此题可以在O(N+kLgN)时间内解决,用maxHeap和stack解决,我还没看,可以参看:https://leetcode.com/discuss/26745/c-solution-with-o-n-klgn-time-using-max-heap-and-stack 和 http://maskray.me/blog/2015-03-27-leetcode-best-time-to-buy-and-sell-stock-iv
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: