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 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).
题解:
若是k很大已经超过了prices.length的时候, 若是按照DP的方法做会浪费时间以及空间. 此时可以直接参照Best Time to Buy and Sell Stock II做法.
Let local[i][j] denotes local maximum profit of up when prices up to i, transaction up to j, the last transaction happend at i.
loacal[i][j] = Math.max(global[i-1][j-1] + Math.max(diff, 0), local[i-1][j] + diff).
global[i-1][j-1] is global maximum profit up to prices i-1, transaction up to j-1. The last transaction happen at prices[i]. If could be diff or 0.
local[i-1][j] is local maximum profit up to pricesi-1, transaction up to j. It already perform j transaction, then the stock sold at prices[i-1] must be sold at prices[i].
Time Complexity: O(prices.length * k), k是最多交易次数.
Space: O(k).
AC Java:
public class Solution {
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0 || k == 0){
return 0;
}
if(k>=prices.length/2){
int res = 0;
for(int i = 1; i < prices.length; i++){
res += Math.max(0, prices[i]-prices[i-1]);
}
return res;
} int[] local = new int[k+1];
int[] global = new int[k+1];
for(int i = 1; i<prices.length; i++){
int diff = prices[i] - prices[i-1];
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(global[j], local[j]);
}
}
return global[k];
}
}
是Best Time to Buy and Sell Stock III的general情况.
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
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- 【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) ...
随机推荐
- Word 2010 给公式添加序号
在写paper的时候,我们常常要给公式编号,难道我们要在公式和最右边的序号之间疯狂按空格键吗,当然不是,我们可以有更高效的方法来完成. 首先我们插入一个1x3的表格,然后调节首尾两个格子的大小,借助标 ...
- OpenCV 2.4.11 VS2012 Configuration
Add in the system Path: C:\opencv\build\x86\vc11\bin; Project->Project Property->Configuration ...
- ajax无刷新获取php后台数据
$.ajax({ url:"result.php", //data:{"page":i}, dataType:"json", beforeS ...
- 绕过杀毒软件抓取windows密码
使用procdump,由于是微软的东西,带微软签名杀软不会报毒. procdump -accepteula -ma lsass.exe lsass.dmp copy出 lsass.dmp到本机. mi ...
- java实现吸血鬼数字
public class Vempire { public static void main(String[] arg) { String[] ar_str1, ar_str2; ; int from ...
- 揭开UTF-8的神秘面纱
UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,又称万国码.由Ken Thompson于1992年创建.现在已经标准化为 ...
- 【iCore3 双核心板】例程二十八:FSMC实验——读写FPGA
实验指导书及代码包下载: http://pan.baidu.com/s/1gerjjxh iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- activeform 配置
<?php $form = ActiveForm::begin([ 'action' => ['/admin/admin/adminadd'], 'id' => 'login-for ...
- 红米note3的wifi断流或假死
红米note3的wifi断流/假死 日常使用note3的时,比如长时间浏览网页,点击一个链接会卡住不动,在等待十几秒之后才恢复.第一反应是不是网络不好?但是这种情况常常出现之后,对比其他的手机,比如价 ...
- int左移32位的行为未定义/Coverity
int左移32位的行为未定义 Coverity 代码静态安全检测 Is Shifting more than 32 bits of a uint64_t integer on an x86 machi ...