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. windows server 2008 R2 计划任务备份系统

    实验环境拓扑图: 实验效果: Windows Server Backup 可以设置备份计划,但只能按日进行备份,不能设置按周或月进行备份.所以,需要使用到 windows Server 2008 R2 ...

  2. Java面向对象的基本概念(对象、封装、继承、多态、抽象、接口、泛型)

    对象:是一个自包含的实体,用一组可识别的特征和行为来标识. 类:具有相同的属性和功能的对象的抽象合集.(类关键字class,首字母大写). 实例:就是一个真实的对象. 实例化:创建对象的过程,关键字是 ...

  3. nginx负载均衡一:基础知识

    基础信息 nginx 的 upstream目前支持 4 种方式的分配  1).轮询(默认)  每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.  2).weight ...

  4. 将本地代码上传github

    建立本地仓库 1.首先进入text文件夹 cd d:text 首先进入text文件夹 2.执行指令:git init 执行git init 初始化成功后你会发现项目里多了一个隐藏文件夹.git 隐藏的 ...

  5. 【01月22日】A股滚动市盈率PE最低排名

    深康佳A(SZ000016) - 滚动市盈率PE:1.55 - 滚动市净率PB:1.03 - 滚动年化股息收益率:4.71% - - - 深康佳A(SZ000016)的历史市盈率走势图 华菱钢铁(SZ ...

  6. web项目加载图片资源

    在web项目中,用户会上传图片,这些图片应该存在服务器硬盘上,而不是存在数据库或者应用程序路径下,在数据库存入文件的路径. 这是一个比较重要的问题,也是开发过程中也解决的问题.当然,我可以跳过,但是成 ...

  7. git 搭建本地仓库

    文档 创建仓库 mkdir project cd project/ git init git remote add origin /d/project/.git // 仓库创建好了 echo hell ...

  8. python 中的 print 函数与 list函数

    print()  函数: 传入单个参数时默认回车换行,关键词 end 可以用来避免输出后的回车(换行), 或者以一个不同的字符串结束输出. >>> a, b = 0, 1 >& ...

  9. WPF 打开指定文件路径的文件资源管理器

    x 需求是想让WPF打开一个指定文件路径的文件夹,但是搜出来的八成都是<打开文件>的这样的↓ Microsoft.Win32.OpenFileDialog open_file = new ...

  10. ASP.NET 前端Ajax获取数据并刷新

    控制器中↓ /// <summary> /// 根据ID来进行展示数据 /// </summary> /// <param name="instru_id&qu ...