Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1e3b38

Browse files
committed
新增 0309.最佳买卖股票时机含冷冻期.md Go解法
1 parent bc77968 commit e1e3b38

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎problems/0309.最佳买卖股票时机含冷冻期.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,40 @@ class Solution:
208208
```
209209

210210
Go:
211+
```go
212+
// 最佳买卖股票时机含冷冻期 动态规划
213+
// 时间复杂度O(n) 空间复杂度O(n)
214+
func maxProfit(prices []int) int {
215+
n := len(prices)
216+
if n < 2 {
217+
return 0
218+
}
219+
220+
dp := make([][]int, n)
221+
status := make([]int, n * 4)
222+
for i := range dp {
223+
dp[i] = status[:4]
224+
status = status[4:]
225+
}
226+
dp[0][0] = -prices[0]
227+
228+
for i := 1; i < n; i++ {
229+
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1] - prices[i], dp[i - 1][3] - prices[i]))
230+
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3])
231+
dp[i][2] = dp[i - 1][0] + prices[i]
232+
dp[i][3] = dp[i - 1][2]
233+
}
234+
235+
return max(dp[n - 1][1], max(dp[n - 1][2], dp[n - 1][3]))
236+
}
237+
238+
func max(a, b int) int {
239+
if a > b {
240+
return a
241+
}
242+
return b
243+
}
244+
```
211245

212246
Javascript:
213247

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /