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 f8fbdd4

Browse files
committed
新增 0714.买卖股票的最佳时机含手续费(动态规划).md Go解法
1 parent e1e3b38 commit f8fbdd4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎problems/0714.买卖股票的最佳时机含手续费(动态规划).md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,37 @@ class Solution:
152152
```
153153

154154
Go:
155+
```go
156+
// 买卖股票的最佳时机含手续费 动态规划
157+
// 时间复杂度O(n) 空间复杂度O(n)
158+
func maxProfit(prices []int, fee int) int {
159+
if len(prices) == 1 {
160+
return 0
161+
}
162+
163+
dp := make([][]int, len(prices))
164+
status := make([]int, len(prices) * 2)
165+
for i := range dp {
166+
dp[i] = status[:2]
167+
status = status[2:]
168+
}
169+
dp[0][0] = -prices[0]
170+
171+
for i := 1; i < len(dp); i++ {
172+
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i])
173+
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee)
174+
}
175+
176+
return dp[len(dp) - 1][1]
177+
}
178+
179+
func max(a, b int) int {
180+
if a > b {
181+
return a
182+
}
183+
return b
184+
}
185+
```
155186

156187
Javascript:
157188
```javascript

0 commit comments

Comments
(0)

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