动态规划——Best Time to Buy and Sell Stock IV
状态转移方程:dp[k][i] = max(dp[k][i-1],dp[k-1][j]+prices[i]-prices[j]) (0<=j<=i)
比如现在正在考虑dp[k][i],选择有两种,一是第i天不操作,二是在第k-1次第j天后进行第k次操作。这个题的状态转移方程相对来说还是比较容易理解的。
class Solution{
public static int maxProfit(int k,int[] prices) {
int nlen = prices.length;
if(nlen<=1)return 0;
else if(k>nlen/2) {
int res = 0;
for(int i = 1;i<nlen;i++)
if(prices[i]>prices[i-1])res+=(prices[i]-prices[i-1]);
return res;
}else {
int temp = 0;
int[][]dp = new int[k+1][nlen];
Arrays.fill(dp[0],0);
for(int i = 0;i<=k;i++)
dp[i][0] = 0;
for(int kt = 1;kt<=k;kt++) {
for(int i = 1;i<nlen;i++) {
temp = 0;
for(int j = 0;j<=i;j++)
temp = temp>(dp[kt-1][j]+prices[i]-prices[j])?temp:(dp[kt-1][j]+prices[i]-prices[j]);
dp[kt][i] = dp[kt][i-1]>temp?dp[kt][i-1]:temp;
}
}
return dp[k][nlen-1];
}
}
}
不过在最后说一句,由于我编写的代码使用了三层for循环,时间复杂度为O(n^3),在LeetCode上仅仅击败了不到9%的人,额,又是一个比较惨淡的数据了。。。。
动态规划——Best Time to Buy and Sell Stock IV的更多相关文章
- Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...
- 动态规划——Best Time to Buy and Sell Stock III
题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果最多进行两次交易,但必须在买进一只股票前清空手中的股票,求最大的收益. 示例 1:Input: [3,3,5,0,0,3 ...
- [LeetCode] 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 ...
- Lintcode393 Best Time to Buy and Sell Stock IV solution 题解
[题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...
- [LeetCode][Java] 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 a ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- i-83.net quadhost子产品
i-83.net 6$一年, 首年半价, 优惠码: APR19-NAT50 加拿大 ---------------------------------------------------------- ...
- (六)循环和控制语句及列表迭代(enumerate)
一.pythoh中while.for.if的循环 嗯.........这个好像没什么好说的,简单粗暴来几个游戏! 1.来玩儿个猜数字游戏,需求:只能猜3次,小了提示小,大了提示大,猜对了游戏结束 3次 ...
- EasyUI整合篇
easy ui combobox getValue 获取不到值问题 必须设置属性showblank: true,否则只能从onSelect事件中获取 $("#ddlType").c ...
- jmeter使用csv传参进行并发测试验证
1.获取到注册接口,添加HTTP信息头管理器.HTTP请求,设置好入参,且检查使用csv文件传参的入参 2.创建csv文件,写入需要传的入参 3.添加CSV Data Set Config 设置配置 ...
- word20170104办签证 Visa application有用的词和句子
有用的词:visa category: 签证类型tourist visa: 旅游签证visa interview: 签证面试multiple entries: 多次往返visa on arrival: ...
- ‘Host’ is not allowed to connect to this mysql server
‘Host’ is not allowed to connect to this mysql server mysql 数据库不允许远程连接 方法一:修改 host 表 进入mysql数据库,选择m ...
- 关于vscode插件 的一些体验
一 vsCode git 首先vscode 继承了 git 在使用git前 先手动创建一个文件夹 用来clone已有项目 然后将 clone下来的项目放入工作区 右上角的小转转就是 pull文件 更 ...
- Spring框架-AOP详细学习[转载]
参考博客:https://blog.csdn.net/qq_22583741/article/details/79589910#4-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85% ...
- 2018-2019-2 网络对抗技术 20165325 Exp1 PC平台逆向破解
2018-2019-2 网络对抗技术 20165325 Exp1 PC平台逆向破解(BOF实验) 实验有三个模块: (一)直接修改程序机器指令,改变程序执行流程: (二)通过构造输入参数,造成BOF攻 ...
- 2018-2019-2 20165325《网络对抗技术》Exp0 Kali安装 Week1
2018-2019-2 20165325<网络对抗技术>Exp0 Kali安装 Week1 一.安装kali VMware上学期已经有了,主要是下载Kali-Linux-2019.1-vm ...