> 文章列表 > Golang每日一练(leetDay0041) 股票买卖4题

Golang每日一练(leetDay0041) 股票买卖4题

Golang每日一练(leetDay0041) 股票买卖4题

目录

121. 买卖股票的最佳时机  🌟

122. 买卖股票的最佳时机 II  🌟🌟

123. 买卖股票的最佳时机 III  🌟🌟🌟

188. 买卖股票的最佳时机 IV  🌟🌟🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


Best-time-to-buy-and-sell-stock(1~4)

121. 买卖股票的最佳时机

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

示例 1:

输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

示例 2:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

提示:

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4

代码1:

package mainimport ("fmt"
)func maxProfit(prices []int) int {maxProfit := 0for i := 0; i < len(prices); i++ {for j := i + 1; j < len(prices); j++ {profit := prices[j] - prices[i]if profit > maxProfit {maxProfit = profit}}}return maxProfit
}func main() {prices := []int{7, 1, 5, 3, 6, 4}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

代码2:

package mainimport ("fmt"
)func maxProfit(prices []int) int {minPrice := 1 << 31maxProfit := 0for _, price := range prices {if price < minPrice {minPrice = price} else if price-minPrice > maxProfit {maxProfit = price - minPrice}}return maxProfit
}func main() {prices := []int{7, 1, 5, 3, 6, 4}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

代码3: 动态规划

package mainimport ("fmt"
)func maxProfit(prices []int) int {minPrices := make([]int, len(prices))minPrices[0] = prices[0]for i := 1; i < len(prices); i++ {minPrices[i] = min(minPrices[i-1], prices[i])}maxProfit := 0for i := 0; i < len(prices); i++ {profit := prices[i] - minPrices[i]if profit > maxProfit {maxProfit = profit}}return maxProfit
}func min(a, b int) int {if a < b {return a}return b
}func main() {prices := []int{7, 1, 5, 3, 6, 4}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

输出:

5
0


122. 买卖股票的最佳时机 II

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。

返回 你能获得的 最大 利润 。

示例 1:

输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。总利润为 4 + 3 = 7 。

示例 2:

输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。总利润为 4 。

示例 3:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。

提示:

  • 1 <= prices.length <= 3 * 10^4
  • 0 <= prices[i] <= 10^4

代码1: 贪心算法

package mainimport ("fmt"
)func maxProfit(prices []int) int {maxProfit := 0for i := 1; i < len(prices); i++ {if prices[i] > prices[i-1] {maxProfit += prices[i] - prices[i-1]}}return maxProfit
}func main() {prices := []int{7, 1, 5, 3, 6, 4}fmt.Println(maxProfit(prices))prices = []int{1, 2, 3, 4, 5}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

 代码2: 动态规划

package mainimport ("fmt"
)func maxProfit(prices []int) int {n := len(prices)if n < 2 {return 0}dp := make([][]int, n)for i := 0; i < n; i++ {dp[i] = make([]int, 2)}dp[0][0] = 0dp[0][1] = -prices[0]for i := 1; i < n; 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[n-1][0]
}func max(a, b int) int {if a > b {return a}return b
}func main() {prices := []int{7, 1, 5, 3, 6, 4}fmt.Println(maxProfit(prices))prices = []int{1, 2, 3, 4, 5}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

输出:

7
4
0


123. 买卖股票的最佳时机 III

给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:prices = [3,3,5,0,0,3,1,4]
输出:6
解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。

示例 2:

输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。   注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。   因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:

输入:prices = [7,6,4,3,1] 
输出:0 
解释:在这个情况下, 没有交易完成, 所以最大利润为 0。

示例 4:

输入:prices = [1]
输出:0

提示:

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^5

代码1:

package mainimport ("fmt"
)func maxProfit(prices []int) int {if len(prices) <= 1 {return 0}buy1, sell1, buy2, sell2 := -prices[0], 0, -prices[0], 0for i := 1; i < len(prices); i++ {buy1 = max(buy1, -prices[i])sell1 = max(sell1, buy1+prices[i])buy2 = max(buy2, sell1-prices[i])sell2 = max(sell2, buy2+prices[i])}return sell2
}func max(a, b int) int {if a > b {return a}return b
}func main() {prices := []int{3, 3, 5, 0, 0, 3, 1, 4}fmt.Println(maxProfit(prices))prices = []int{1, 2, 3, 4, 5}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

代码2: 动态规划

package mainimport ("fmt"
)func maxProfit(prices []int) int {n := len(prices)if n <= 1 {return 0}dp := make([][]int, n)for i := 0; i < n; i++ {dp[i] = make([]int, 3)}dp[0][0] = -prices[0]dp[0][1] = 0dp[0][2] = 0for i := 1; i < n; i++ {dp[i][0] = max(dp[i-1][0], dp[i-1][2]-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])}return dp[n-1][1]
}func max(a, b int) int {if a > b {return a}return b
}func main() {prices := []int{3, 3, 5, 0, 0, 3, 1, 4}fmt.Println(maxProfit(prices))prices = []int{1, 2, 3, 4, 5}fmt.Println(maxProfit(prices))prices = []int{7, 6, 4, 3, 1}fmt.Println(maxProfit(prices))
}

输出:

6
4
0


188. 买卖股票的最佳时机 IV

给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入:k = 2, prices = [2,4,1]
输出:2
解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。

示例 2:

输入:k = 2, prices = [3,2,6,5,0,3]
输出:7
解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。

提示:

  • 0 <= k <= 100
  • 0 <= prices.length <= 1000
  • 0 <= prices[i] <= 1000

代码: 动态规划

package mainimport ("fmt"
)func maxProfit(k int, prices []int) int {if k == 0 || len(prices) == 0 {return 0}n := len(prices)if k > n/2 {// 相当于k为正无穷return maxProfitInf(prices)}dp := make([][][]int, n)for i := 0; i < n; i++ {dp[i] = make([][]int, k+1)for j := 0; j <= k; j++ {dp[i][j] = make([]int, 2)}}for i := 0; i < n; i++ {for j := 1; j <= k; j++ {if i == 0 {// 处理边界dp[i][j][0] = 0dp[i][j][1] = -prices[i]continue}dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1]+prices[i])dp[i][j][1] = max(dp[i-1][j][1], dp[i-1][j-1][0]-prices[i])}}return dp[n-1][k][0]
}func maxProfitInf(prices []int) int {n := len(prices)dp_i_0, dp_i_1 := 0, -prices[0]for i := 1; i < n; i++ {dp_i_0 = max(dp_i_0, dp_i_1+prices[i])dp_i_1 = max(dp_i_1, dp_i_0-prices[i])}return dp_i_0
}func max(a, b int) int {if a > b {return a}return b
}func main() {prices := []int{2, 4, 1}fmt.Println(maxProfit(2, prices))prices = []int{3, 2, 6, 5, 0, 3}fmt.Println(maxProfit(2, prices))
}

输出:

2
7


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏