Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
解题思路:
https://leetcode.com/discuss/18330/is-it-best-solution-with-o-n-o-1%E3%80%82
本题是Best Time to Buy and Sell Stock系列最难的一道,
参考Java for LeetCode 123 Best Time to Buy and Sell Stock III解法二的思路,四个数组可以压缩为两个数组,JAVA实现如下:
public int maxProfit(int k, int[] prices) {
if (k == 0 || prices.length < 2)
return 0;
if (k > prices.length / 2) {
int maxProfitII = 0;
for (int i = 1; i < prices.length; ++i)
if (prices[i] > prices[i - 1])
maxProfitII += prices[i] - prices[i - 1];
return maxProfitII;
}
int[] buy=new int[k];
int[] sell=new int[k];
for(int i=0;i<buy.length;i++)
buy[i]=Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++)
for (int j = k - 1; j >= 0; j--) {
sell[j] = Math.max(sell[j], buy[j] + prices[i]);
if (j == 0)
buy[j] = Math.max(buy[j], -prices[i]);
else
buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]);
}
return sell[k - 1];
}
Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】的更多相关文章
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【刷题-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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Java for LeetCode 121 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 ...
- 188. Best Time to Buy and Sell Stock IV (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 122 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 ...
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- python 变量命名规范
python源码和其他一些书籍,命名各种个性,没有一个比较统一的命名规范.于是总结了一些,供参考. 模块名: 模块应该使用尽可能短的.全小写命名,可以在模块命名时使用下划线以增强可读性.同样包的命名也 ...
- Java编程思想学习(八) 内部类
可以将一个类的定义放在另一个类的定义内部,这就是内部类. 内部类的定义是简单的,但是它的语法确实很是复杂,让人不是很好理解.下面就内部类做一个小结. 一.内部类的分类 总的来讲内部类分为普通内部类,匿 ...
- poj3207 2-SAT入门
一开始题意没读懂 = = 题意:比如说对于表盘上a到b.c到d都要连边,这两个边不能交叉.这两个边要么都在圆内要么都在圆外,而且可以是曲线= = 比如这种情况:(Reference:http://bl ...
- Linux Process/Thread Creation、Linux Process Principle、sys_fork、sys_execve、glibc fork/execve api sourcecode
相关学习资料 linux内核设计与实现+原书第3版.pdf(.3章) 深入linux内核架构(中文版).pdf 深入理解linux内核中文第三版.pdf <独辟蹊径品内核Linux内核源代码导读 ...
- [IOS Delegate和协议]
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/25655443 作者:小马 代理和协议的语法这里不赘述,自己查资料. 这个demo的 ...
- java导出txt文本
页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...
- HD1561The more, The Better(树形DP+有依赖背包)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【js】JSON.stringify 语法实例讲解
语法: JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为 ...
- CentOS 6.5(6.4)安装过程图文教程
CentOS 6.4是最新的出的系统,这里分享下安装教程,有些设置大部分教程没出现过,特分享下,方便需要的朋友 1.首先,要有一张CentOS 6.4的安装介质,使用介质启动电脑出现如下界面 界面说明 ...
- vagrant 启动错误
Stderr: VBoxManage.EXE: error: Failed to create the VirtualBox object!VBoxManage.EXE: error: Code E_ ...