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);
                
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值