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. 微信小程序:设置页面计时自动跳转

    一.功能描述 当出发某一事件后,希望在规定的时间后自动执行另一事件,比如页面跳转功能. 二.代码实现 使用setTimeout函数,单位为毫秒ms setTimeout(function(){ wx. ...

  2. 洛谷NOIp热身赛题解

    洛谷NOIp热身赛题解 A 最大差值 简单树状数组,维护区间和.区间平方和,方差按照给的公式算就行了 #include<bits/stdc++.h> #define il inline # ...

  3. java对象是如何创建的

    虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符合引用代表的类是否已被加载.解析和初始化过.如果没有,那必须先执行相应的类加载过程. 在类加 ...

  4. 初识IT行业,人生苦短,我学python

    第一次写,我也不知道该怎么写.只有慢慢的去体会大神们的见解与看法. Python是一个较强的脚本语言,而Java是强类型的编程语言.为了更好的入门,我没有去选择强类型语言的Java,而选择使用Pyth ...

  5. Jenkins自动化测试

    Jenkins自动化测试 一个持续集成的基本原则是构建应该是可验证的.你必须能够客观地确定一个特定的构建是否准备就绪构建过程的下一个阶段,最便捷的方式做到这一点是使用自动化测试.如果没有适当的自动化测 ...

  6. OpenGL学习笔记(6) 基础光照的计算方法

    这个笔记只是冯氏光照模型下漫反射光以及镜面光照的计算方式的笔记 基础光照 基础光照分为环境光,漫反射光,镜面光照 环境光 环境光是一个常量,表示在没有光源的情况下物体的光 漫反射光 漫反射光分量的计算 ...

  7. (2) English Learning

      数词 数词有基数词和序数词两种.英语的数词可以作句子的主语.宾语.表语和定语. 基数词:表示数目的词叫基数词. 1. 英语中常用的基数词有:除了图片上的,还有以下一些 1000→one(a) th ...

  8. docker入门使用教程

    Docker概念 Docker是开发人员和系统管理员 使用容器开发,部署和运行应用程序的平台.使用Linux容器部署应用程序称为容器化.容器不是新的,但它们用于轻松部署应用程序. 容器化越来越受欢迎, ...

  9. python-模拟掷骰子,两个筛子数据可视化

    """ 作者:zxj 功能:模拟掷骰子,两个筛子数据可视化 版本:3.0 日期:19/3/24 """ import random impo ...

  10. 机器人平台框架Yarp - Yet another robot platform

    简介 ROS有强大和易用的特性,用的人很多,目前已经推出2.0版本,有相关的官网和论坛.然而其缺点也比较明显. 只能基于Ubuntu系统,且一个ROS版本只能对应一个具体的Ubuntu版本    通信 ...