Skip to content

Commit

Permalink
Time: 5 ms (40.6%), Space: 14.3 MB (21.37%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Feb 9, 2025
1 parent 97a2fd6 commit 3436905
Showing 1 changed file with 3 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
func maxProfit(prices []int, fee int) int {
dp := make([][2]int, len(prices))
dp[0][1] = -prices[0]
dp[0][1] = - prices[0]

for i := 1; i < len(prices); i++ {
dp[i][0] = max(dp[i-1][0], prices[i] + dp[i-1][1] - fee)
dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i])
}

return dp[len(prices) - 1][0]
}

func max(a, b int) int {
if a > b {
return a
}

return b
}

0 comments on commit 3436905

Please sign in to comment.