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. 设计模式,python延迟计算缓存模式

    之前已经发过单独的缓存,这也算一种模式. from __future__ import print_function import functools class lazy_property(obje ...

  2. [UFLDL] *Sparse Representation

    Deep learning:二十九(Sparse coding练习) Deep learning:二十八(使用BP算法思想求解Sparse coding中矩阵范数导数) Deep learning:二 ...

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

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

  4. 终端IO(上)

    一.综述 终端IO有两种不同的工作方式: 规范方式输入处理.在这种方式中,终端输入以行为单位进行处理.对于每个读要求,终端驱动程序最多返回一行. 非规范方式输入处理.输入字符不以行为单位进行装配 如果 ...

  5. mysql基础---->mybatis的批量插入(一)

    这里面记录一下使用mybatis处理mysql的批量插入的问题,测试有可能不准.只愿世间风景千般万般熙攘过后,字里行间,人我两忘,相对无言. mybatis的批量插入 我们的测试主体类是springb ...

  6. 关于 oh-my-zsh 插件的使用(以 Sublime Text 为例)

    这里不讲 oh-my-zsh 是什么.也不讲 oh-my-zsh 插件的工作原理(太深奥,不懂 ). 讲一讲作为一个初学者,在使用过程中遇到的问题以及解决方法. 1 缘起 Ubuntu下,编辑/预览 ...

  7. 深入web开发之webserver/servlet容器

    可能按照书上的demo,自己就能做个小型网站,但是在并发下是什么情况呢?生成了多少对象?对象的关系又是什么?这些问题都要慢慢弄清楚. ------作为后端工程师,不仅要会增删改查,还要了解servle ...

  8. 连接mysql 数据库时出现2003 -can't connect to mysql server on 'localhost'(100038)的问题

    通过eclipse连接数据库是出现了以下问题 可能是由于数据库未开启造成的,你需要手动开启数据库. 手动开启数据库的方法: 1 win+r键后,在cmd中输入services.msc,点击确定 点击左 ...

  9. Entity Framework DbSet<T>之Include方法与IQueryable<T>扩展方法Include的使用

    Entity Framework使用Code First方式时,实体之间已经配置好关系,根据实际情况某些情况下需要同时获取导航属性,比如获取商品的同时需要获取分类属性(导航属性),或者基于优化方面考虑 ...

  10. win10 Java环境变量,hadoop 环境变量

    妈呀,今天又重装了系统.需要装上java环境. 安装环境老百度,然后老掉坑.(path 路经) 1,新建环境变量 JAVA_HOME 2, 新建 CLASSPATH 环境变量 .;%JAVA_HOME ...