LeetCode——Best Time to Buy and Sell Stock IV
Description:
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/feliciafay/article/details/45128771
local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),
global[i][j]=max(local[i][j],global[i-1][j])
public class Solution {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
if(len < 2) {
return 0;
}
if(k > len) {
return bigProfit(prices);
}
int[] local = new int[k+1];
int[] global = new int[k+1];
Arrays.fill(local, 0);
Arrays.fill(global, 0);
for(int i=0; i<len-1; i++) {
int diff = prices[i+1] - prices[i];
for(int j=k; j>=1; j--) {
local[j] = Math.max(global[j-1]+(diff>0?diff:0), local[j]+diff);
global[j] = Math.max(global[j], local[j]);
}
}
return global[k];
}
public int bigProfit(int[] prices) {
int res = 0;
for(int i=0; i<prices.length-1; i++) {
if(prices[i] < prices[i+1]) {
res += prices[i+1] - prices[i];
}
}
return res;
}
}
LeetCode——Best Time to Buy and Sell Stock IV的更多相关文章
- [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 ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- 【dp】leetcode Best Time to Buy and Sell Stock IV
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次, ...
- 【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] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
随机推荐
- DataRowState、AcceptChanges、RejectChanges综合使用示例:实现DataGridView数据的增、删、改
下面的示例中,通过一个程序,演示使用DataRowState.AcceptChanges.RejectChanges,实现DataGridView数据的增.删.改. 一.界面设计 二.代码实现 usi ...
- json格式在ruby和rails中的注意事项
#虚拟网络拓扑的json数据 def topodata #@vnic = Vnic.all #flash.now[:notice] = 'Message sent!' #flash.now[:aler ...
- Tomcat负载均衡和集群环境的搭建
实现此集群的方法参考了网上的很多文章,但由于很多文章都表明是原创的,故无法知道整个操作流程的真正作者是谁.下面就是我用我们真实的项目去实现这个过程.同时修复这过程中一些问题.以下的所有步骤均为亲自测试 ...
- Error -27740: WSA_IO_pending
Error -27740: WSA_IO_pendingAction.c(198): Error -27791: Server **** has shut down the connection pr ...
- Oracle 存储过程错误之PLS-00201: 必须声明标识符
转自:http://blog.csdn.net/u010678947/article/details/20702149 错误: ORA-06550: 第 1 行, 第 7 列: PLS-00201: ...
- mac for appium环境安装
之前写过windows 安装appium环境步骤. 1. 需求的前置条件如下 (mac 自动git.ruby.brew命令): 2. java 环境 3. git 环境 4. ruby环境 5. b ...
- nest(inner) class
嵌套类,摘自: http://www.ntu.edu.sg/home/ehchua/programmin/java/J4a_GUI.html A nested class has these prop ...
- QButtonGroup:按钮类的非可视化容器,默认可实现按钮的子类实例的单选。
QButtonGroup The QButtonGroup class provides a container to organize groups of button widgets. QButt ...
- 如何在浏览器控制台(console)里输出彩色样式调试信息
console.log(XX,XX,XX) log 的第一个参数声明第二.第三个参数的作用,第二个参数就是样式,第三个参数是要输出的字符串 console.log("%c%s", ...
- Mockito单元测试框架学习
基本使用方法: http://zhongl.iteye.com/blog/296136 一.问题:如何将mock的类自动注入到待测类,特别是在没有setter方法的情况下. 解答: 前提:待测的ser ...