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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36688622/article/details/89343278