【发布时间】:2021-11-29 16:02:20
【问题描述】:
我能够找到解决方案“找到最大利润的价值”。
const pricesEachDay = [1,2,5,8,7,1,2,3]
const mainFunction = (pricesEachDay) => {
let buyPrice = pricesEachDay[0];
let bestProfit = 0;
for (const price of pricesEachDay) {
const currentProfit = price - buyPrice;
buyPrice = Math.min(buyPrice, price);
bestProfit = Math.max(bestProfit, price - buyPrice);
}
return bestProfit;
}
我的问题是。我将如何处理它,如果我想找到我买入股票和卖出股票的那一天的指数,并将它与最大利润一起返还。所以我可以在 React 中突出显示它。 谢谢!
【问题讨论】:
-
应该是“buyPrice”,抱歉打错了。已更正
标签: javascript arrays reactjs algorithm