美文网首页
(6/1/16)Leetcode 122. Best Time

(6/1/16)Leetcode 122. Best Time

作者: Flashpacker | 来源:发表于2016-06-02 12:12 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/agckdttx.html