309.买卖股票的最佳时机含冷冻期

买卖股票的最佳时机含冷冻期

给定一个整数数组 prices,其中 prices[i] 表示第 i 天的股票价格。卖出股票后,你无法在第二天买入股票(即冷冻期为 1 天)。返回获取最大利润。

解析

1
2
3
4
5
6
7
8
9
10
var maxProfit = function (prices) {
let hold = -prices[0], sold = 0, rest = 0;
for (let i = 1; i < prices.length; i++) {
const newHold = Math.max(hold, rest - prices[i]);
const newSold = hold + prices[i];
const newRest = Math.max(rest, sold);
hold = newHold; sold = newSold; rest = newRest;
}
return Math.max(sold, rest);
};

309.买卖股票的最佳时机含冷冻期
https://leetcode.lz5z.com/309.best-time-to-buy-and-sell-stock-with-cooldown/
作者
tickli
发布于
2024年8月22日
许可协议