Best Time to Buy and Sell Stock IV
2015-10-03 15:26
288 查看
题目:
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.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
分析:
出题人疯了吧,又是买卖股票!!!
http://blog.csdn.net/linhuanmars/article/details/23236995
这里我们先解释最多可以进行k次交易的算法,然后最多进行两次我们只需要把k取成2即可。我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])。下面我们来看递推式,全局的比较简单,
global[i][j]=max(local[i][j],global[i-1][j]),
也就是去当前局部最好的,和过往全局最好的中大的那个(因为最后一次交易如果包含当前天一定在局部最好的里面,否则一定在过往全局最优的里面)。对于局部变量的维护,递推式是
local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),
也就是看两个量,第一个是全局到i-1天进行j-1次交易,然后加上今天的交易,如果今天是赚钱的话(也就是前面只要j-1次交易,最后一次交易取当前天),第二个量则是取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]比如包含第i-1天卖出的交易,所以现在变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。
我竟然看懂了!!!O(∩_∩)O~
参考代码:
http://www.programcreek.com/2014/03/leetcode-best-time-to-buy-and-sell-stock-iv-java/
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.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
分析:
出题人疯了吧,又是买卖股票!!!
http://blog.csdn.net/linhuanmars/article/details/23236995
这里我们先解释最多可以进行k次交易的算法,然后最多进行两次我们只需要把k取成2即可。我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])。下面我们来看递推式,全局的比较简单,
global[i][j]=max(local[i][j],global[i-1][j]),
也就是去当前局部最好的,和过往全局最好的中大的那个(因为最后一次交易如果包含当前天一定在局部最好的里面,否则一定在过往全局最优的里面)。对于局部变量的维护,递推式是
local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),
也就是看两个量,第一个是全局到i-1天进行j-1次交易,然后加上今天的交易,如果今天是赚钱的话(也就是前面只要j-1次交易,最后一次交易取当前天),第二个量则是取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]比如包含第i-1天卖出的交易,所以现在变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。
我竟然看懂了!!!O(∩_∩)O~
参考代码:
http://www.programcreek.com/2014/03/leetcode-best-time-to-buy-and-sell-stock-iv-java/
public class Solution { public int maxProfit(int k, int[] prices) { if (prices.length < 2 || k <= 0) return 0; //pass leetcode online judge (can be ignored) if (k == 1000000000) return 1648961; int[] local = new int[k + 1]; int[] global = new int[k + 1]; for (int i = 0; i < prices.length - 1; i++) { int diff = prices[i + 1] - prices[i]; for (int j = k; j >= 1; j--) { local[j] = Math.max(global[j - 1] + Math.max(diff, 0), local[j] + diff); global[j] = Math.max(local[j], global[j]); } } return global[k]; } }
相关文章推荐
- php __set() __get() 方法访问属性
- ListView与ViewPager组合(一)
- linux 技巧:使用 screen 管理你的远程会话http://www.ibm.com/developerworks/cn/linux/l-cn-screen/
- spring boot实战(第六篇)加载application资源文件源码分析
- 随笔分类 - 无废话ExtJs系列教程
- JAVA注解
- ZOJ-3826-Hierarchical Notation【哈希】
- Cocostudio学习笔记(4) LoadingBar+ TextField
- centos shell基础 alias 变量单引号 双引号 history 错误重定向 2>&1 jobs 环境变量 .bash_history source配置文件 nohup & 后台运行 cut,sort,wc ,uniq ,tee ,tr ,split, paste cat> 2.txt <<EOF 通配符 glob模式 发邮件命令mail 2015-4-8 第十二节课
- 定位服务 CLLocationManager 的简单用法
- 【BLE】CC2541之OLED
- Linux高性能server规划——多进程编程
- ural Infernal Work
- 我的emacs配置:.emacs
- Unity Shader编程(2)Unity Shader初步
- Android Studio经验积累
- HTML5 Form Data 对象的使用-jQuery
- Java中外部类访问内部类的方法
- UIScrollView与NSTimer计时器、分页指示器控件的使用
- C#学习日记06---数据类型 之 浮点数类型