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

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example 1:

Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

Input: [3,2,6,5,0,3], k = 2
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
  Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. 使用动态规划
  public int maxProfit(int k, int[] prices) {//dp mytip
if(null==prices||0==prices.length||0>=k){
return 0;
}
int max =0;
if(k>prices.length){//若k过大 优化
for (int i = 0; i < prices.length-1; i++) {
if(prices[i]<prices[i+1]){
max+= prices[i+1]-prices[i];
}
}
}
else{
int[][][] states = new int[prices.length][k+1][2];//状态表示第i个数在第j此交易中,有无股票时(0为无,1为有)的利益;//因为只保存上一个数时的利益,所以states可优化为[k+1][2]
for(int i=0;i<=k;i++){//初始化第1个数的状态
states[0][i][1]=-prices[0];
}
for(int i=1;i<prices.length;i++){
for(int j=0;j<=k;j++){
if(j==0){
states[i][j][0] = states[i-1][j][0];//防止j-1溢出
}
else{
states[i][j][0] = Math.max(states[i-1][j][0],states[i-1][j-1][1]+prices[i]);
}
states[i][j][1] = Math.max(states[i-1][j][1],states[i-1][j][0]-prices[i]);
}
}
for(int i=0;i<=k;i++){
max = max>states[prices.length-1][i][0]?max:states[prices.length-1][i][0];
}
} return max;
}

优化空间

public int maxProfit(int k, int[] prices) {//dp my
if(null==prices||0==prices.length||0>=k){
return 0;
}
int max =0;
if(k>prices.length){
for (int i = 0; i < prices.length-1; i++) {
if(prices[i]<prices[i+1]){
max+= prices[i+1]-prices[i];
}
}
}
else{
int[][] states = new int[k+1][2];
states[0][1] = -prices[0];
for(int i=0;i<=k;i++){
states[i][1]=-prices[0];
}
for(int i=1;i<prices.length;i++){
for(int j=0;j<=k;j++){
states[j][1] = Math.max(states[j][1],states[j][0]-prices[i]);
if(j==0){
states[j][0] = states[j][0];
}
else{
states[j][0] = Math.max(states[j][0],states[j-1][1]+prices[i]);
} }
}
for(int i=0;i<=k;i++){
max = max>states[i][0]?max:states[i][0];
}
} return max;
}

相关题

买卖股票的最佳时间1 LeetCode121 https://www.cnblogs.com/zhacai/p/10429264.html

买卖股票的最佳时间2 LeetCode122 https://www.cnblogs.com/zhacai/p/10596627.html

买卖股票的最佳时间3 LeetCode123 https://www.cnblogs.com/zhacai/p/10645571.html

买卖股票的最佳时间冷冻期 LeetCode309 https://www.cnblogs.com/zhacai/p/10655970.html

买卖股票的最佳时间交易费 LeetCode714 https://www.cnblogs.com/zhacai/p/10659288.html

LeetCode-188.Best Time to Buy and Sell Stock IV的更多相关文章

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

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

  3. LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  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

    Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...

  6. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

  8. 188. Best Time to Buy and Sell Stock IV (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 188. Best Time to Buy and Sell Stock IV leetcode解题笔记

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. 188. Best Time to Buy and Sell Stock IV——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. Cisco VLAN ACL配置

    什么是ACL? ACL全称访问控制列表(Access Control List),主要通过配置一组规则进行过滤路由器或交换机接口进出的数据包, 是控制访问的一种网络技术手段, ACL适用于所有的被路由 ...

  2. [原创]找不到mswinsck.ocx的解决办法

    mswinsck.ocx,是在运行程序或者游戏时,系统弹出错误提示“ 找不到mswinsck.ocx”,或者“ 没有找到 mswinsck.ocx”时,说明您系统中缺失这个OCX文件或者该OCX文件没 ...

  3. 使用InstallAnywhere7.1制作Java exe程序安装包

    [转[使用InstallAnywhere7.1制作Java exe程序安装包 使用InstallAnywhere7.1制作Java exe程序安装包 对于已经完成的Java应用程序开发项目,从商业化角 ...

  4. 磨刀不误砍柴工——统一日志系统 Log4Net/ExceptionLess

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫必须注明原文地址:www.cnblogs.com/tdws . 一.   写在前面 本文Log4Net介绍了基础的方式,大数据量生产环境不能使用,中等 ...

  5. 图片相似原理--Java实现

    前阵子在阮一峰的博客上看到了这篇<相似图片搜索原理>博客,就有一种冲动要将这些原理实现出来了. Google "相似图片搜索":你可以用一张图片,搜索互联网上所有与它相 ...

  6. [Unity3D] 04 - Event Manager

    message消息管理 脚本与GameObject的关系 被显式添加到 Hierarchy 中的 GameObject 会被最先实例化,GameObject 被实例化的顺序是从下往上. GameObj ...

  7. 如何处理MySQL每月5亿的数据

    第一阶段:1,一定要正确设计索引2,一定要避免SQL语句全表扫描,所以SQL一定要走索引(如:一切的 > < != 等等之类的写法都会导致全表扫描)3,一定要避免 limit 100000 ...

  8. 大杂烩 -- equals、hashCode联系与区别

    基础大杂烩 -- 目录 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Equals 1.默认情况(没有覆盖equals方 ...

  9. Navicat -- Oracle -- 错误锦集

    ORA:connection to server failed,probable Oracle Net admin error 解决的方案是: oci.dll的版本不对  从 http://www.o ...

  10. ansj分词原理

    ansj第一步会进行原子切分和全切分,并且是在同时进行的.所谓原子,是指短句中不可分割的最小语素单位.例如,一个汉字就是一个原子.全切分,就是把一句话中的所有词都找出来,只要是字典中有的就找出来.例如 ...