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).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
public class Solution {
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length == 0) return 0;
int len = prices.length;
if (k >= len / 2) return quickSolve(prices);
int[][] dp = new int[k + 1][len];
for (int i = 1; i <= k; i++) {
int tmpMax = -prices[0];
for(int j = 1; j < len; j ++){
dp[i][j] = Math.max(dp[i][j - 1], prices[j] + tmpMax);
tmpMax = Math.max(tmpMax, dp[i - 1][j - 1] - prices[j]);
}
}
return dp[k][len - 1];
} public int quickSolve(int[] prices){
int result = 0;
for(int i = 1; i < prices.length; i ++){
if(prices[i] - prices[i - 1] > 0) result += prices[i] - prices[i - 1];
}
return result;
}
}
tmpMax means the maximum profit of just doing at most i-1 transactions, using at most first j-1 prices, and buying the stock at price[j] - this is used for the next loop.
public class Solution {
public int maxProfit(int k, int[] prices) {
if(prices == null || prices.length < 2 || k == 0) return 0;
int len = prices.length;
if(k * 2 >= len){//actually we can do as many transactions as we want
int result = 0;
for(int i = 1; i < len; i ++){
if(prices[i] - prices[i - 1] > 0) result += prices[i] - prices[i - 1];
}
return result;
}else{//transactions time is limited
int[][] dp = new int[k + 1][len];
for(int i = 1; i <= k; i ++){
int MaxPre = -prices[0];
for(int j = 1; j < len; j ++){
dp[i][j] = Math.max(dp[i][j - 1], MaxPre + prices[j]);
MaxPre = Math.max(MaxPre, dp[i - 1][j - 1] - prices[j]);
}
}
return dp[k][len - 1];
}
}
}
Best Time to Buy and Sell Stock IV的更多相关文章
- 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之动态规划(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
Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...
- [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 ...
- 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 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 ...
- 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 ...
随机推荐
- CF1111E Tree 树链剖分,DP
CF1111E Tree 过年了,洛咕还没爬这次的题,先放个CF的链接吧. 补个LG传送门. 对于每个询问点\(x\),设它的祖先即不能和它放在同一个集合中的点的个数为\(f[x]\),设\(dp[i ...
- 基于Vue的弹框实例
看到博客的人,请养成写博客的习惯,不会不会,就怕曾经会过,现在想不起来了,一起加油.................... 让学习真的成为一种习惯,同时要注意身体 <!DOCTYPE html ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- check the manual that corresponds to your MySQL server version for the right syntax to use near
一.问题 mysql插入数据时报错 sql如下 insert into t_sysconfig (servercode,key,value,remark,updatetime) values (&qu ...
- JavaScript 的HTML转义方法 html_encode 和 html_decode
此方法用来将用户输入内容中的尖括号.引号等进行转义
- 关于springcloud的一些问题总结.txt
@Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCo ...
- linux下实现压测-html报表生成-控制台参数优化【jmeter】
jmeter - 单机压测 - 命令行模式-html报表生成-控制台参数优化 一/ 准备工作 1.压力机安装并配置好 jdk 2.调试好程序脚本 再上传到 linux下 3.进入jmeter bin ...
- PHP核心技术——异常和错误处理
PHP只有手动抛出异常后才能捕获异常 $a = null; try { $a = 5/0; echo $a,PHP_EOL; } catch (exception $e) { $e -> get ...
- 最新Microsoft Edge!使用chromium内核
2018年11月,微软宣布其Edge浏览器将采用Chromium引擎,意味着微软的Edge浏览器以失败告终. 但令人振奋的是,新版Edge也许会“死而复生”.在使用了Chromium内核后,Edge各 ...
- cd命令详解
基础命令学习目录首页 cd 进入用户主目录: cd ~ 进入用户主目录: cd - 返回进入此目录之前所在的目录: cd .. 返回上级目录(若当前目录为“/“,则执行完后还在“/":&qu ...