【leetcode】Array——Best Time to Buy and Sell Stock I/II/III

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.

思路:DP 只能交易一次,所以记录目前为止最低的price,在记录当前位置与最低price之间的差价,return max

代码:

public int maxProfit(int[] prices) {
	if(prices==null||prices.length==0)
		return 0;
    int max =0;
    int lowestP = prices[0];
    for(int i=1;i<prices.length;i++){
    	if(prices[i]>lowestP){
    		if(max<prices[i]-lowestP)
    			max=prices[i]-lowestP;
    	}
    	else
    		lowestP=prices[i];
    }
	
	return max;
}

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

思路:这次没有交易次数的限制了,所以直接找到增价的总和totle即可

代码:

public int maxProfit(int[] prices) {
	int totle=0;
	if(prices.length==0||prices==null)
		return 0;
    for(int i=1;i<prices.length;i++){
    	if(prices[i]>prices[i-1])
    		totle+=(prices[i]-prices[i-1]);
    }
	return totle;
}

Best Time to Buy and Sell StockIII

题目:

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

思路:一开始的想法,记录每个增值阶段的增价的幅度,取最大的2各,求和即可。但是wrong answer。细想,的确“有瑕疵”!

看了leetcode上面的解题:

https://leetcode.com/discuss/18330/is-it-best-solution-with-o-n-o-1

代码:

public class Solution {
    public int maxProfit(int[] prices) {
        int hold1 = Integer.MIN_VALUE, hold2 = Integer.MIN_VALUE;
        int release1 = 0, release2 = 0;
        for(int i:prices){                              // Assume we only have 0 money at first
            release2 = Math.max(release2, hold2+i);     // The maximum if we've just sold 2nd stock so far.
            hold2    = Math.max(hold2,    release1-i);  // The maximum if we've just buy  2nd stock so far.
            release1 = Math.max(release1, hold1+i);     // The maximum if we've just sold 1st stock so far.
            hold1    = Math.max(hold1,    -i);          // The maximum if we've just buy  1st stock so far. 
        }
        return release2; ///Since release1 is initiated as 0, so release2 will always higher than release1.
    }
}
Profit of stock 2 is based on existing profit of release of stock 1.  Profit of release is based on profit at hold. The first line is actually using hold2 for i-1, the 2nd line update hold2 to i, also using release1 for i-1, then the 3rd line update release1 to i, using hold1 for i-1, the 4th line update it at last.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值