Leetcode 簡單三十一 買股票的最佳時機

買股票的最佳時機:

PHP:

40ms。

class Solution {

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

 

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