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的更多相关文章

  1. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  2. 【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 ...

  3. 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. ...

  4. 【刷题-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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. idea 和 WebStorm 配置 http代理 并更换主题

    proxy,http,socks5 当前 idea 主题为:(idea 自带) idea 编辑器的主题颜色字体为:(网上下载的 jar 包) 因为今天在安装下面这个主题时需要在 idea的 plugi ...

  2. 遗留系统:IT攻城狮永远的痛

    我常常觉得我们非常幸运,我们现在所处的时代是一个令人振奋的时代,我们进入了软件工业时代.在这个时代里,我们进行软件开发已经不再是一个一个的小作坊,我们在进行着集团化的大规模开发.我们开发的软件不再是为 ...

  3. 英文样式教师求职简历免费word模板

    10款英文样式教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度网盘, ...

  4. Qt-网易云音乐界面实现-1 窗口隐藏拖拽移动,自定义标题栏

    最近也换了公司,也换了新的工作,工资也象征性的涨了一点点,但是最近心里还是慌慌,不知道为什么,没有那种踏实感,感觉自己随时可以被抛弃的感觉.感觉自己在荒废时间,也感觉自己在浪费生命. 为了让自己在被抛 ...

  5. nginx解析漏洞,配置不当,目录遍历漏洞环境搭建、漏洞复现

    nginx解析漏洞,配置不当,目录遍历漏洞复现 1.Ubuntu14.04安装nginx-php5-fpm 安装了nginx,需要安装以下依赖 sudo apt-get install libpcre ...

  6. JUC——延迟队列

    所谓的延迟队列最大的特征是它可以自动通过队列进行脱离,例如:现在有一些对象被临时保存着,但是有可能该集合对象是一个公共对象,那么里面的某些数据如果不在使用的时候就希望其可以在指定的时间达到后自动的消失 ...

  7. linux_connect_mysql

    原文来自 https://www.cnblogs.com/lywy510/p/3615710.html #include <stdio.h> #include <stdlib.h&g ...

  8. GodMode | Windows上帝模式

    最近在网上学习到了一些Windows的隐藏功能,今天我就来说说GodMode模式吧. 借鉴:https://jingyan.baidu.com/article/90bc8fc853c38bf65264 ...

  9. 半年收入超2亿RMB 独立游戏开发者的艰苦创业路

    一款叫做<监狱建筑师>的模拟经营游戏,目前在Steam平台获得了3000万美元(近2亿元)以上的收入.这款游戏由英国独立工作室Introversion Software发布,而团队最困难的 ...

  10. Yii2 创建新项目目录

    默认的高级应用模板包括三个应用 backend – 应用的后台 frontend – 应用的前台 console – 应用的控制台应用 那么如果我们要在增加应用呢?比如在加一个手机端的应用,或者后台和 ...