/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算最大收益
* @param prices int整型一维数组 股票每一天的价格
* @return int整型
*/
function maxProfit(prices) {
// write code here
let len = prices.length;
if (len < 1) {
return 0;
}
let ans = 0;
let min = prices[0];
for (let i = 0; i < len; i++) {
min = prices[i];
// 找到最后一个上升的下标
while (i + 1 < len && prices[i + 1] > prices[i]) {
i++;
}
let cnt = prices[i] - min;
ans += cnt;
}
return ans;
}
module.exports = {
maxProfit: maxProfit,
};

京公网安备 11010502036488号