Leetcode 簡單三十二 買賣股票的最佳時機II

買賣股票的最佳時機II

PHP:

28ms。貪心算法。

class Solution {

    /**
     * @param Integer[] $prices
     * @return Integer
     */
    function maxProfit($prices) {
        $profit = 0;
        for($i = 0;$i < count($prices)-1;$i++){
            $pro = $prices[$i+1] - $prices[$i];
            if($pro > 0) $profit += $pro;
        }
        return $profit;
    }
}

 

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