一、题目地址
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
二、思路地址
https://www.bilibili.com/video/BV1Sv411t7eG?from=search&seid=2076531796429670426
三、具体代码
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let max = 0;
let minPrice = prices[0];//变量minPrice为当前最低的价格
for(let i=1; i<prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]);
// 每一天的利润最大化 = 前一天的利润最大化 || (今天的价格 - 这几天的最小值)
max = Math.max(max, prices[i] - minPrice);
}
return max;
};