From f7adb3ab9f6bfd06489ee006237215d19db5a514 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Fri, 10 Feb 2023 10:01:40 +0800 Subject: [PATCH] Update stock notes --- notes/src/stock.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/notes/src/stock.md b/notes/src/stock.md index 6f31fbc..f84347a 100644 --- a/notes/src/stock.md +++ b/notes/src/stock.md @@ -16,3 +16,18 @@ - `dp[0][0] = -prices[0]` - `dp[0][1] = 0` - 遍历顺序:从前往后 + +## [123.买卖股票的最佳时机III](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/) + +- `dp[i][j][k]` 是第 `i` 笔交易中,第 `j` 天,是否持有股票时的总资产 +- 递推公式: + - `dp[0][i][0] = max(dp[0][i - 1][0], -prices[i])` + - `dp[0][i][1] = max(dp[0][i - 1][1], dp[0][i][0] + prices[i])` + - `dp[1][i][0] = max(dp[1][i - 1][0], dp[0][i - 1][1] - prices[i])` + - `dp[1][i][1] = max(dp[1][i - 1][1], dp[0][i - 1][0] + prices[i])` +- 初始化: + - `dp[i][0][0] = -prices[0]` + - `dp[i][0][1] = 0` +- 遍历顺序: + - 先遍历 `i` + - 再遍历 `j` 和 `k`