【Golang主题学习月】周末肝了几道动态规划题,发了一个超细腻的教学版,反响很不错哦,接下来我会使用两种语言进行编码刷题,分别是GO和JAVA,各位菁英们,坚持刷题吧。
什么题可以选择动态规划来做?
1.计数
- 有多少种方式走到右下角
- 有多少种方法选出k个数是的和是sum
2.求最大值最小值
- 从左上角走到右下角路径的最大数字和
- 最长上升子序列长度
3.求存在性
- 取石子游戏,先手是否必胜
- 能不能选出k个数使得和是sum
leecode 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 <= 105
0 <= prices[i] <= 105
--
参考代码
GO语言版
func maxProfit(prices []int) int {
pLen := len(prices)
if pLen == 0{
return 0
}
result := 0
profit := make([][3][2]int, pLen)
/**
*三维数组
*profit[ii][kk][jj]
*ii 第 ii 天, kk 股票操作了几次 , jj 是否有股票
*最多可以完成 两笔 交易: kk 可以为 0 1 2 次操作 , jj可以为 0 ,1
**/
/**
*第一天 初始化
*第 1 天 操作 k 次 没有股票,所以初始值为 0
*第 1 天 操作i 次 有股票, 所以初始值为 - prices[0]
**/
profit[0][0][0], profit[0][0][1] = 0, -prices[0]
profit[0][1][0], profit[0][1][1] = 0, -prices[0]
profit[0][2][0], profit[0][2][1] = 0, -prices[0]
//注意 买 卖 都进行一次算一次操作 k + 1,单独 买入 不算完成一次操作
for i:=1; i<pLen; i++{
//第 i 天 0 次交易 没有股票最大利润 = 第 i-1 天 0 次交易 没有股票最大利润
profit[i][0][0] = profit[i-1][0][0]
//第 i 天 0 次交易 有股票最大利润 = max(第 i-1 天 0 次交易 有股票最大利润 , 第 i-1 天 0 次交易 无股票最大利润 - 当天股票价格prices[i](买入))
profit[i][0][1] = max(profit[i-1][0][1], profit[i-1][0][0] - prices[i])
//# 第 i 天 1 次交易 无股票最大利润 = max(第 i-1 天 1次交易 无股票最大利润 , 第 i-1 天 0 次交易 有股票最大利润 + 当天股票价格prices[i](卖出))
profit[i][1][0] = max(profit[i-1][1][0], profit[i-1][0][1] + prices[i])
// # 第 i 天 1 次交易 有股票最大利润 = max(第 i-1 天 1 次交易 有股票最大利润 , 第 i-1 天 1 次交易 无股票最大利润 - 当天股票价格prices[i](买入))
profit[i][1][1] = max(profit[i-1][1][1], profit[i-1][1][0] - prices[i])
//# 第 i 天 2 次交易 无股票最大利润 = max(第 i-1 天 2次交易 无股票最大利润 , 第 i-1 天 1 次交易 有股票最大利润 + 当天股票价格prices[i](卖出))
profit[i][2][0] = max(profit[i-1][2][0], profit[i-1][1][1] + prices[i])
}
result = max(profit[pLen-1][0][0], max(profit[pLen-1][1][0], profit[pLen-1][2][0]))
return result
}
func max(m, n int)int{
if m>n{
return m
}
return n
}
复制代码
java版
public int maxProfitDP(int[] prices) {
if (prices == null || prices.length <= 1) return 0;
int[][][] dp = new int[prices.length][2][3];
int MIN_VALUE = Integer.MIN_VALUE / 2;//因为最小值再减去1就是最大值Integer.MIN_VALUE-1=Integer.MAX_VALUE
//初始化
dp[0][0][0] = 0;//第一天休息
dp[0][0][1] = dp[0][1][1] = MIN_VALUE;//不可能
dp[0][0][2] = dp[0][1][2] = MIN_VALUE;//不可能
dp[0][1][0] = -prices[0];//买股票
for (int i = 1; i < prices.length; i++) {
dp[i][0][0] = 0;
dp[i][0][1] = Math.max(dp[i - 1][1][0] + prices[i], dp[i - 1][0][1]);
dp[i][0][2] = Math.max(dp[i - 1][1][1] + prices[i], dp[i - 1][0][2]);
dp[i][1][0] = Math.max(dp[i - 1][0][0] - prices[i], dp[i - 1][1][0]);
dp[i][1][1] = Math.max(dp[i - 1][0][1] - prices[i], dp[i - 1][1][1]);
dp[i][1][2] = MIN_VALUE;
}
return Math.max(0, Math.max(dp[prices.length - 1][0][1], dp[prices.length - 1][0][2]));
}
复制代码
其实这道题跟动态规划-买卖股票的最佳时机-超简单的题目|Go主题月 是差不多的,我们同样可以设置一个maxprofit,这样的话,就可以将空间复杂度降低为O(1).
我们知道,我们操作最多就四个步骤,买一次,卖一次,在买一次,在卖一次,将这四次最大收益分别设置为:
buy1、sell1、buy2、sell2
对于买一次来说: 我们就求某天买一次的价格和买一次的收益的最大值,也就是:
buy1 = max(buy1, -prices[i])
对于sell1 而言,在第 i 天我们可以不进行任何操作,保持不变,也可以在只进行过一次买操作的前提下以 prices[i] 的价格卖出股票,那么sell1
sell1 =max(sell1, buy1+prices[i])
那么buy2和sell2自然就是:
buy2 = max(buy2, sell1-prices[i])
sell2 =max(sell2, buy2+prices[i])
而且,同一天是可以买入和卖出的。
func maxProfit(prices []int) int {
buy1, sell1 := -prices[0], 0
buy2, sell2 := -prices[0], 0
for 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
}
复制代码
❤️❤️❤️❤️
非常感谢人才们能看到这里,如果这个文章写得还不错,觉得有点东西的话 求点赞👍 求关注❤️ 求分享👥 对帅气欧巴的我来说真的 非常有用!!!
如果本篇博客有任何错误,请批评指教,不胜感激 !
文末福利,最近整理一份面试资料《Java面试通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:GitHub github.com/Tingyu-Note…,更多内容关注公号:汀雨笔记,陆续奉上。
近期评论