121. Best Time to Buy and Sell Stock 1,2

  1. Best Time to Buy and Sell Stock
public class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length==0) return 0;
        int max=0,min=prices[0];
        for(int i=0;imin){
                max=Math.max(max,prices[i]-min);
            }else min=prices[i];
        }
        return max;
    }
}
  1. Best Time to Buy and Sell Stock II
public class Solution {
    public int maxProfit(int[] prices) {
        int total=0;
        for(int i=0;iprices[i]) total+=prices[i+1]-prices[i];
        }
        return total;
    }
}

你可能感兴趣的:(121. Best Time to Buy and Sell Stock 1,2)