买卖股票的最佳时机

记录最低的价格,每次计算最大的利润,返回即可

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
let minPrice = prices[0];
let maxProfitNum = 0;
for (let i = 1; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]);
maxProfitNum = Math.max(maxProfitNum, prices[i] - minPrice);
}
return maxProfitNum;
};

本站由 ao 使用 Stellar 1.29.1 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。