27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Se...

 

Best Time to Buy and Sell Stock 

(onlineJudge: https://oj.leetcode.com/problems/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. 若以后价格不变,不买不卖。 1. 更价格低,重新买。2. 价格升高,假定抛售,更新一下利润值。

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int buy = 0x7fffffff, maxProfile = 0;
        for(int i = 0; i < prices.size(); ++i) {
            if(prices[i] == buy) continue;
            if(prices[i] < buy) { buy = prices[i]; continue; }
            else maxProfile = max(maxProfile, prices[i]-buy);
        }
        return maxProfile;
    }
};

 

Best Time to Buy and Sell Stock II

 (onlineJudge: https://oj.leetcode.com/problems/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).

思想:求出所有非递减序列两端绝对值之和。我贴在 leedcode 的代码和证明:

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n = prices.size();
        if(n == 0) return 0;
        int start = 0, profile = 0;
        for(int i = 1; i < n; ++i) {
            if(prices[i] < prices[i-1]) {
                profile += prices[i-1] - prices[start];
                start = i;
            }
        }
        profile += prices[n-1] - prices[start];
        return profile;
    }
};
/*********************** *provement ***************/
/*Explain the code I pasted above: 

From left to right I find out every subsequence that not exist decrease. 

such as: l ... k1 ...k2 ... h (l <=...<= k1 <= ... k2 <= ... <= h)

In this sequence: ( k1-l ) + ( h-k2 ) = ( h-l ) - ( k2-k1 ) <= ( h-l ); 

So (h - l) will be the maximum profit in this days.

Another case:

L1 ...d1... H1 K2 ...k... K3 L2 ...d2... H2 (L1 <=... H1 > K2 >=...k >=... K3 > L2 <=... H2 )

K2 ... K3 is not exist increase sequence.

then for any k in that position,

( k-d1 ) + ( d2-k ) <= ( K2-L1 ) + ( H2-K3 ) < ( H1-L1 ) + ( H2-L2 ) 

In my code, variant "start" is the start of every no decrease sequence.*/

 A little Adjustment.

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int start = 0, profile = 0;
        for(size_t i = start+1; i < prices.size(); ++i) {
            if(prices[i] < prices[i-1]) {
                profile += prices[i-1] - prices[start];
                start = i;
            }
            else if(i+1 == prices.size()) 
                profile += prices[i] - prices[start];
        }
        return profile;
    }
};

 

Best Time to Buy and Sell Stock III

 (onlineJudge: https://oj.leetcode.com/problems/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).

 思想:动态规划。 记录下从各位置(含)开始之前的最大利润和此时开始到最后的最大利润。

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        vector<int> preProfile(prices.size()+2, 0), postProfile(prices.size()+2, 0);
        
        int minPrice = 0x7fffffff;
        for(size_t i = 1; i <= prices.size(); ++i) {
            minPrice = min(minPrice, prices[i-1]);
            preProfile[i] = max(prices[i-1] - minPrice, preProfile[i-1]);
        }
        
        int maxPrice = 0;
        for(int i = prices.size(); i >= 1; --i) {
            maxPrice = max(maxPrice, prices[i-1]);
            postProfile[i] = max(maxPrice - prices[i-1], postProfile[i+1]);
        }
        
        int maxProfile = 0;
        for(size_t i = 1; i <= prices.size(); ++i) 
            maxProfile = max(maxProfile, preProfile[i] + postProfile[i]);
        return maxProfile;
    }
};

 

转载于:https://www.cnblogs.com/liyangguang1988/p/3939420.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值