diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index b875ed8a..98ad66bc 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -196,6 +196,33 @@ class Solution: ``` Go: +```go +// 买卖股票的最佳时机Ⅱ 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int) int { + dp := make([][]int, len(prices)) + status := make([]int, len(prices) * 2) + for i := range dp { + dp[i] = status[:2] + status = status[2:] + } + dp[0][0] = -prices[0] + + for i := 1; i < len(prices); i++ { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]) + } + + return dp[len(prices) - 1][1] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` ```go func maxProfit(prices []int) int { diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index bce7b83c..1e2c1510 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -346,7 +346,38 @@ const maxProfit = prices => { }; ``` +Go: +> 版本一: +```go +// 买卖股票的最佳时机III 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int) int { + dp := make([][]int, len(prices)) + status := make([]int, len(prices) * 4) + for i := range dp { + dp[i] = status[:4] + status = status[4:] + } + dp[0][0], dp[0][2] = -prices[0], -prices[0] + + for i := 1; i < len(prices); i++ { + dp[i][0] = max(dp[i - 1][0], -prices[i]) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]) + dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] - prices[i]) + dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i]) + } + + return dp[len(prices) - 1][3] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` ----------------------- diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index a069fbe6..10bc141f 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -272,6 +272,41 @@ class Solution: return dp[2*k] ``` Go: +版本一: +```go +// 买卖股票的最佳时机IV 动态规划 +// 时间复杂度O(kn) 空间复杂度O(kn) +func maxProfit(k int, prices []int) int { + if k == 0 || len(prices) == 0 { + return 0 + } + + dp := make([][]int, len(prices)) + status := make([]int, (2 * k + 1) * len(prices)) + for i := range dp { + dp[i] = status[:2 * k + 1] + status = status[2 * k + 1:] + } + for j := 1; j < 2 * k; j += 2 { + dp[0][j] = -prices[0] + } + + for i := 1; i < len(prices); i++ { + for j := 0; j < 2 * k; j += 2 { + dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]) + dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]) + } + } + return dp[len(prices) - 1][2 * k] +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` ```go func maxProfit(k int, prices []int) int { diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 525f8cef..9f991a7f 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -204,6 +204,40 @@ class Solution: ``` Go: +```go +// 最佳买卖股票时机含冷冻期 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) +func maxProfit(prices []int) int { + n := len(prices) + if n < 2 { + return 0 + } + + dp := make([][]int, n) + status := make([]int, n * 4) + for i := range dp { + dp[i] = status[:4] + status = status[4:] + } + dp[0][0] = -prices[0] + + for i := 1; i < n; i++ { + dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1] - prices[i], dp[i - 1][3] - prices[i])) + dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]) + dp[i][2] = dp[i - 1][0] + prices[i] + dp[i][3] = dp[i - 1][2] + } + + return max(dp[n - 1][1], max(dp[n - 1][2], dp[n - 1][3])) +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` Javascript: diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index b5981950..4721248c 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -149,14 +149,16 @@ class Solution: ``` Go: -```Go +```go +// 买卖股票的最佳时机含手续费 动态规划 +// 时间复杂度O(n) 空间复杂度O(n) func maxProfit(prices []int, fee int) int { n := len(prices) dp := make([][2]int, n) dp[0][0] = -prices[0] for i := 1; i < n; i++ { - dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]-fee) - dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i]) + dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i] - fee) + dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i]) } return dp[n-1][1] }