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

来源:互联网 发布:matlab矩阵求转置 编辑:程序博客网 时间:2024/06/05 19:18

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