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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章