(6/1/16)Leetcode 122. Best Time to Buy and Sell Stock II

Medium, 用时10分钟

  1. 一切从简原则, pin-point在需要关注的点
  2. Accumulation累加
  3. Greedy
public class Solution {
    public int maxProfit(int[] prices) {
        //8:58
        if(prices.length == 1) return 0;
        int length = prices.length;
        int profit = 0;
        for (int i = 0; i < length - 1; i++){
            if(prices[i] < prices[i+1]) {
                profit = profit + prices[i+1] - prices[i]; 
            }
        }   
        return profit;
    }
}

你可能感兴趣的:((6/1/16)Leetcode 122. Best Time to Buy and Sell Stock II)