Leetcode - Greedy Algorithm - Best Time to Buy and Sell Stock

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.

解法1:最大值-最小值。

解法2:最大子序列和

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.empty())
            return 0;
        vector<int>::iterator beg=prices.begin();
        int maxPro=0,curMinPrice=*beg;
        for(;beg!=prices.end();beg++)
        {
            maxPro=max(maxPro, *beg-curMinPrice);
            curMinPrice=min(curMinPrice, *beg);
        }
        return maxPro;
    }
};

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.empty())
            return 0;
        int maxPro=0,tempPro=0;
        vector<int>::iterator beg=prices.begin();
        int curPrice=*beg;
        for(;beg!=prices.end();beg++)
        {
            tempPro+=*beg-curPrice;
            curPrice=*beg;
            if(tempPro<0)
                tempPro=0;
            if(tempPro>maxPro)
                maxPro=tempPro;
        }
        return maxPro;
    }
};



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

解析:求相邻数组元素增长的和。

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        if(prices.empty())
            return 0;
        vector<int>::iterator beg=prices.begin();
        int maxPro=0, curPrice=*beg;
        for(; beg!=prices.end();beg++)
        {
            if(*beg>curPrice)
                maxPro+=*beg-curPrice;
            curPrice=*beg;
        }
        return maxPro;
    }
};



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

解析:见注释,该算法将空间复杂度降低为O(1)

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // 空间负杂度为O(1),时间复杂度为O(n)
        if(prices.size()<=1)
            return 0;
        int minPriceIndex=0,maxPriceIndex=0;
        int maxPro=0,maxTemp=0,curPriceIndex=0,curMinPriceIndex=0;
        // 首先找出只做一次交易最大的收益maxPro,并记录买入时下标minPriceIndex和卖出时下标maxPriceIndex
        for(int i=1;i<prices.size();i++)
        {
            maxTemp+=prices[i]-prices[curPriceIndex];
            curPriceIndex=i;
            if(maxTemp<0)
            {
                maxTemp=0;
                curMinPriceIndex=curPriceIndex;
            }
            if(maxTemp>maxPro)
            {
                maxPro=maxTemp;
                minPriceIndex=curMinPriceIndex;
                maxPriceIndex=i;
            }
        }
        // 在买入下标minPriceIndex之前最大的收益maxProAhead
        int maxProAhead=0,curMinPriceAhead=prices[0];
        for(int i=1;i<minPriceIndex;i++)
        {
            maxProAhead=max(maxProAhead,prices[i]-curMinPriceAhead);
            curMinPriceAhead=min(prices[i], curMinPriceAhead);
        }
        // 再卖出下标maxPriceIndex之后最大的收益maxProBehind
        int maxProBehind=0;
        if(maxPriceIndex+1<prices.size())
        {
            int curMinPriceBehind=prices[maxPriceIndex+1];
            for(int i=maxPriceIndex+1;i<prices.size();i++)
            {
                maxProBehind=max(maxProBehind,prices[i]-curMinPriceBehind);
                curMinPriceBehind=min(prices[i], curMinPriceBehind);
            }
        }
        // 再买入之后到卖出之前,最大的跌幅minProMid
        int minProMid=0,curMaxPrice=prices[minPriceIndex];
        for(int i=minPriceIndex+1;i<maxPriceIndex;i++)
        {
            minProMid=min(minProMid, prices[i]-curMaxPrice);
            curMaxPrice=max(prices[i],curMaxPrice);
        }
        // max1表示maxPriceIndex之前两次最大的收益和
        // max2表示minPriceIndex之后和maxPriceIndex之间,除去最大跌幅的收益
        // max3表示minPriceIndex之后两次最大的收益和
        int max1=maxProAhead+maxPro,max2=maxPro-minProMid,max3=maxPro+maxProBehind;
        return max1>(max2>max3?max2:max3)?max1:(max2>max3?max2:max3);
                
    }
};



发布了76 篇原创文章 · 获赞 21 · 访问量 6万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览