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 ...
随机推荐
- bind支持mysql
最近打算将bind的记录信息存入到数据库中去,网上找了下,原来早有老外写好了mysql-bind的补丁,重新编译bind即可实现bind支持mysql存储.(http://mysql-bind.sou ...
- Beyond Compare 4 使用30天后过期续用方法
Beyond Compare 4 使用30天后过期续用方法 windows上的Beyond Compare 4软件过期了,两个方法: 方案一: 找到Beyond Compare 4安装目录,安装时默认 ...
- P3254——DP&&入门
题目 给定一个$n \times m$的$01$矩形,选择其中为$1$的位置,要求互不相邻,问方案数. 解决方案 直接dp因为状态较多,数组很难直接表示出来,我们采用二进制状态压缩存储. 用$dp[i ...
- 错误调试以及debug的使用
/*定义 .search 搜索*/ $.fn.UiSearch=function(){ var ui=$(this); //任何地方都可以使用断点调试:debugger; //调试时,可以在控制台输入 ...
- 自动上传本地图片和word图片(word图片需使用从word粘贴功能)
在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...
- 小米oj 找小"3"(数位dp)
找小"3" 序号:#40难度:困难时间限制:1000ms内存限制:10M 描述 给定一个奇数n,可得到一个由从1到n的所有奇数所组成的数列,求这一数列中数字3所出现的总次数.例如 ...
- 小米oj 帮小学生排队(排序+插入)
帮小学生排队 序号:#18难度:有挑战时间限制:1000ms内存限制:10M 描述 用一个数组表示一群正在排队的小学生,每个小学生用一对整数 H, K 来表示:H 表示这个小学生的身高,K 表示这个 ...
- linux下如何编辑txt文档
利用vi命令 [root@bogon a]# vi a.txt 打开a.txt文档 vi a.txt后,键盘敲i,可以进入编辑模式, 输入完内容后按ESC键,键盘输入 :wq 可以保存并退出 ...
- codeforces997C
Sky Full of Stars CodeForces - 997C On one of the planets of Solar system, in Atmosphere University, ...
- Linux网络编程六、报文解析(1)
一.pcap文件解析 依赖的库:libpcap,头文件:pcap/pcap.h 获取pcap文件:tcpdump,-i:指定监听接口,默认配置好的最小的号码的接口.-w:指定存入文件,将原始报文存入指 ...