var maxProfit = function (prices) {
if (prices.length == 0) {
return 0;
}
var minProfit = prices[0];
var maxProfit = 0;
for (var i = 0; i < prices.length; i++) {
if (prices[i] < minProfit) {
minProfit = prices[i];
}else if(prices[i] - minProfit > maxProfit){
maxProfit = prices[i] - minProfit;
}
}
return maxProfit;
}
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法
于 2022-08-04 19:55:51 首次发布