动态规划——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 ...
随机推荐
- Java(18) 集合框架
一.集合框架 Collectoin Map List set HashMap ArrayList LinkedList ...
- ccf 201503-5 最小花费 这题交上去只有10分嗨!求大佬的题解啊
问题描述 C国共有n个城市.有n-1条双向道路,每条道路连接两个城市,任意两个城市之间能互相到达.小R来到C国旅行,他共规划了m条旅行的路线,第i条旅行路线的起点是si,终点是ti.在旅行过程中,小R ...
- 中国 A 股纳入 MSCI
1 .什么是 MSCI MSCI 是美国指数编制公司 --- 美国明晟公司的简称. 是一家股权,固定资产,对冲基金,股票市场指数的供应商. MSCI 旗下编制了多种指数,他们把全球股票市场分成发达国家 ...
- linux oops调试
参考文章: arm 指令定位错误 https://blog.csdn.net/songcdut/article/details/41383483 linux mips指令学习 https://www. ...
- BH1750FVI调试
在写此博客之前已经看了几遍数据手册了,现在已经调试成功了,可以读出来数据,还有不如意的地方,写此博客整理下思路. 1.BH1750fvi介绍. 这是一个16bit的数字传感器,使用I2C作为通信接口, ...
- Abd学习笔记
Abd学习笔记 V快捷键:转正坐标 Tab快捷键:切换xyz或是长度角度 空格键快捷键:切换长度或弧度 Enter快捷键:确定方向x或y O快捷键:做辅助线 E:切换平面,分别有t,f,s Ra:创建 ...
- 【转】详解web.xml中元素的加载顺序
顺序为: context-param --> listeners --> filters --> servlets(如DispatcherServlet等) 详见<https: ...
- Windows【端口被占用,杀死想啥的端口】
windows 两步方法 netstat -ano | findstr "8080" taskkill /pid 4136-t -f linux 两步方法 ps -ef | gre ...
- 关于 git 本地创建 SSH Key 遇到的一点问题(①file to save the key & ②the authenticity of host...)
背景 由于想测试一下 SSH Key 创建的路径(.ssh 目录路径)对于不同位置 git 项目是否有效. 比如,.ssh 默认在 C:\[users]\[username] 目录下,而项目 proj ...
- Python——函数,模块,简单文件读写
函数(function)定义原则: 最大化代码重用,最小化代码冗余,流程符合思维逻辑,少用递归; 函数的定义方法: def function_name(param_1, param_2): ..... ...