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. BZOJ2439【中山市选2011】序列

    题面 题解 设$f[i]$表示将$[1,i]$修改为递增的最小代价, $g[i]$表示将$[i,n]$修改为递减的最小代价. $L[i]$表示将$[1,i]$修改为倒$\text V$的代价 $$ \ ...

  2. 【LG3246】[HNOI2016]序列

    [LG3246][HNOI2016]序列 题面 洛谷 题解 60pts 对于每个位置\(i\),单调栈维护它往左第一个小于等于它的位置\(lp_i\)以及往右第一个小于它的位置\(rp_i\). 那么 ...

  3. pandas:对字符串类型做差分比较

    1. 问题需求 某种行为最常发生时段.最少发生时段与X天前是否一致 需求变形:判断上下行数据是否一致 2. 预备知识 2.1 Series.ne(Series) 判断两个Series是否相等 impo ...

  4. $watch和$observe的使用

    $observe 是Attribute对象的一个方法,用来监听DOM中属性值的变化.比如 attr1="{{name}}" Attribute定义在directive中的link函 ...

  5. SpringBoot日记——日志框架篇

    在项目的开发中,日志是必不可少的一个记录事件的组件,所以也会相应的在项目中实现和构建我们所需要的日志框架. 而市面上常见的日志框架有很多,比如:JCL.SLF4J.Jboss-logging.jUL. ...

  6. lua中table的常用方法

    转载:https://blog.csdn.net/Fenglele_Fans/article/details/83627021 1:table.sort() language = {"lua ...

  7. shell解析ini格式文件

    功能 本脚本实现了ini文件中的查询修改指定value 百度云连接地址 链接:https://pan.baidu.com/s/12_T5yST7Y3L1H4_MkVEcvA 密码:fo5p 解压后先看 ...

  8. Docker 入门之docker容器创建

    使用docker容器的大多数人都是因为想要隔离不同运行环境的差异,使得自己的应用能更好的移植和部署.那么我们来看看掌握docker需要掌握哪些方面. 1,搭建docker环境 2,编译镜像并将其运行成 ...

  9. 基于Linux-3.9.4内核增加简单的时间片轮转功能

    简单的时间片轮转多道程序内核代码 原创作品转载请注明出处https://github.com/mengning/linuxkernel/ 作者:sa18225465 一.安装 Linux-3.9.4 ...

  10. time命令详情

    基础命令学习目录首页 原文链接:https://blog.csdn.net/adaptiver/article/details/6596143?utm_source=blogxgwz3 linux下t ...