Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
股票问题:
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: [2,4,1], k = 2
输出: 2
解释: 在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
示例 2:
输入: [3,2,6,5,0,3], k = 2
输出: 7
解释: 在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
一、在第i天,手上没有股票,有两种原因:
1、第i-1天的时候就没有,第i天休息了,什么都没干
2、第i-1天的时候有股票,但是第i天把股票卖了
二、在第i天,手上有股票,有两种原因:
1、第i-1天的时候就没有,第i天啥都没干
2、第i-1天的时候没有股票,第i天买入了一支 另外注意,如果k>prices.length/2的时候,k就变成无限制次数的交易了。
那就可以用贪心or不限制k次数的dp来求解。
不然会爆内存。
class Solution {
public int maxProfit(int k, int[] prices) {
if(prices==null || prices.length==0) return 0;
if(k>prices.length/2){
return maxProfit(prices);
}
int[][][] dp = new int[prices.length][2][k+1];
for (int i = 0; i < k+1; i++) {
dp[0][0][i] = 0;
dp[0][1][i] = -prices[0];
}
for (int i = 1; i < prices.length; i++) {
for (int k1 = 1; k1 <= k; k1++) {
dp[i][0][k1] = Math.max(dp[i-1][0][k1],dp[i-1][1][k1]+prices[i]);
dp[i][1][k1] = Math.max(dp[i-1][1][k1],dp[i-1][0][k1-1]-prices[i]);
}
}
return dp[prices.length-1][0][k];
}
public int maxProfit(int[] prices) {
if(prices==null || prices.length==0) return 0;
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i-1][1]+prices[i],dp[i-1][0]);
dp[i][1] = Math.max(dp[i-1][0]-prices[i],dp[i-1][1]);
}
return dp[prices.length-1][0];
}
}
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)的更多相关文章
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- [Swift]LeetCode122. 买卖股票的最佳时机 II | Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [Swift]LeetCode123. 买卖股票的最佳时机 III | Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [Swift]LeetCode121. 买卖股票的最佳时机 I | Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. 买卖股票的最佳时机( Best Time to Buy and Sell Stock)
题目地址:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 解题思路一:暴力求解法 根据题目我们可以知道,我们知道最大 ...
- [Swift]LeetCode188. 买卖股票的最佳时机 IV | Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
随机推荐
- Springboot打包成jar包形式发布
1.修改配置文件pom.xml 添加打包形式设置为jar形式 <packaging>jar</packaging> 2.在build标签内添加内容如下 finalname为打包 ...
- UVALive 6859——凸包&&周长
题目 链接 题意:在一个网格图上,给出$n$个点的坐标,用一个多边形包围这些点(不能接触,且多边形的边只能是对角线或直线),求多边形的最小周长. 分析 对于每个点,我们考虑与之相邻的4个点.一共由 $ ...
- Acwing-286-选课(树上DP)
链接: https://www.acwing.com/problem/content/288/ 题意: 学校实行学分制. 每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分. 学校开设了 N ...
- 【VS调试】VS调试的时候使用IP地址,局域网的设备可以访问并调试
使用IIS Express调试,只能通过 http://localhost:端口 进行访问 客户端的设备如何才能通过 http://ip地址:端口 访问后台程序进行调试呢? 第一步,打开项目属性, ...
- 移动端H5开发问题记录
1. 当弹出键盘时,会改变页面高度,影响页面样式 通过window.onsize事件可以控制键盘弹出或消失的时候的样式 var h = document.body.scrollHeight // 用o ...
- springbootdruidmybatismysql多数据源事务管理
springboot+druid+mybatis+mysql+多数据源事务管理 分布式事务在java中的解决方案就是JTA(即Java Transaction API):springboot官方提供了 ...
- 「BZOJ 5010」「FJOI 2017」矩阵填数「状压DP」
题意 你有一个\(h\times w\)的棋盘,你需要在每个格子里填\([1, m]\)中的某个整数,且满足\(n\)个矩形限制:矩形的最大值为某定值.求方案数\(\bmod 10^9+7\) \(h ...
- codeforces865C
Gotta Go Fast CodeForces - 865C You're trying to set the record on your favorite video game. The gam ...
- From 7.29 To 8.4
From 7.29 To 8.4 大纲 英语按时背 做点思维题 可能还有时间学点东西, 这周我也不知道应该干什么 7.29 上午考试, 终于有一回不是自闭的考试了 题目比较简单, 就不说了 7.30 ...
- 2.RabbitMq-持久化
RabbitMq-消息持久化 问题:怎样保证消息不因生产者gg而丢失我们知道了如何在消费者的角度保证消息不丢失,但如果生产者gg了呢,消息同样会丢失,生产者gg后会默认丢弃所有的消息,除非告诉它某些消 ...