买股票的最佳时机:

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

 

相关文章:

  • 2022-12-23
  • 2021-08-05
  • 2021-10-27
  • 2021-12-07
  • 2021-12-06
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2021-11-04
  • 2021-12-26
  • 2021-10-29
  • 2021-08-26
相关资源
相似解决方案