leetcode:Best Time to Buy and Sell Stock I II II

Best Time to Buy and Sell Stock I 

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.

Subscribe to see which companies asked this question

题目:给你每天股票的价格,求一次买入一次出手后的最大利润

1.从前往后遍历,0-i天买入,第i天出手时,最多可赚 prices[i] - low ,low是到第i天为止,最低的价格

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ret=0;
        if(prices.size()<=0)
            return ret;
        int low=prices[0];
        for(int i=0;i<prices.size();i++){
            low=low>prices[i]?prices[i]:low;
            ret=(prices[i]-low)>ret?prices[i]-low:ret;
        }
        return ret;
    }
};

2,转自 http://www.cnblogs.com/remlostime/archive/2012/11/06/2757434.html
从后往前遍历,第i天买入过后, i + 1 ~ n卖掉,我最多可以赚max(ans, maxPrice - prices[i])
类似动态规划的思想(只是没有存储每一步的结果),到第i天买入,那么我能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的。找最大股价的问题可以在找第i+1~n天的最大利润时顺便记录
复制代码
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (prices.size() == 0)
            return 0;
            
        int maxPrice = prices[prices.size()-1];
        int ans = 0;
        for(int i = prices.size() - 1; i >= 0; i--)
        {
            maxPrice = max(maxPrice, prices[i]);
            ans = max(ans, maxPrice - prices[i]);
        }
        
        return ans;
    }
};

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

题意:可以买卖多次
思路:从波谷到波峰所赚的利润值,等于 连续的prices[i+1]-prices[i] 累加和
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size()<=1)
            return 0;
        int ret=0;
        for(int i=0;i<prices.size()-1;i++){
            int tmp=prices[i+1]-prices[i];
            ret+=tmp>0?tmp:0;
        }
        return ret;
    }
};

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

最多两次买卖
思路:将prices 拆成两部分,【0-i】,【i-n】对这两部分分别调用

Best Time to Buy and Sell Stock I 中的计算方法求最大利润,再把两个最多利润相加

拆分位置n种,对两部分别求最大利润的复杂度为n,所以总的时间复杂度O(n^2)
由于  Best Time to Buy and Sell Stock I 求最大利润时已经把各个位置的最大利润都计算过了,只是 只存了所有位置的最大利润,没有存各个位置的利润。现在只要用空间换时间,把各个位置的最大利润值存储下来就可以减少运算了。----动态规划
【0-i】调用Best Time to Buy and Sell Stock I中第一种方法从前往后遍历,flg1[i] 表示【1-i】区间的最大利润
【i-n】调用Best Time to Buy and Sell Stock I中第二种方法,从后往前遍历,flg2[i] 表示【i-n】区间的最大利润
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ret=0;
        int len=prices.size();
        if(len<=1)
            return 0;
        
        vector<int> flg1(len,0);//flg1[i] 表示【1-i】区间的最大利润
        vector<int> flg2(len,0);//flg2[i] 表示【i-n】区间的最大利润
        int max=prices[len-1];
        int low=prices[0];
        int cur=0;
        for(int i=0;i<prices.size();i++){
            low=low>prices[i]?prices[i]:low;
            cur=(prices[i]-low)>cur?(prices[i]-low):cur;
            flg1[i]=cur;
        }
        cur=0;
        for(int i=len-1;i>=0;i--){
            max=max>prices[i]?max:prices[i];
            cur=(max-prices[i])>cur?max-prices[i]:cur;
            flg2[i]=cur;
        }
        
        for(int i=0;i<len;i++){
            ret=ret<flg1[i]+flg2[i]?flg1[i]+flg2[i]:ret;    
        }
        return ret;
    }
};

参考: http://www.tuicool.com/articles/rMJZj2



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值