LeetCode-188.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 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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 【刷题-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 ...
- 【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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Scala学习笔记——内建控制结构
Scala的内建控制结构包括:if.while.for.try.match和函数调用 1.if表达式 //常见的写法 var filename = "name" if (!args ...
- cocos2d-x游戏引擎核心(3.x)----启动渲染流程
(1) 首先,这里以win32平台下为例子.win32下游戏的启动都是从win32目录下main文件开始的,即是游戏的入口函数,如下: #include "main.h" #inc ...
- 【!Important】Zookeeper用来做什么的,有几种类型的节点
zookeeper=文件系统+通知机制 一.Zookeeper提供了什么 1.文件系统 Zookeeper维护一个类似文件系统的数据结构 每个子目录项如NameService都被称为znoed,和文件 ...
- C# 如何提取字符串中的数字
下面讲解如何在字符串当中抓取到数字 方法一.使用正则表达式 1.纯数字提取 string str = "提取123abc提取"; //我们抓取当前字符当中的123 string r ...
- win2008在组件服务中未找到office组件服务
在win2003系统,cmd中输入 dcomcnfg ,组件服务里面找到office的组件服务,但win2008 R2 64位操作系统需要输入comexp.msc -32 tks:http://www ...
- 6 CLR实例构造器
引用类型构造器 如果我们没有定义实例构造器,那么编译器会为我们默认产生一个无参构造器. 实例对象初始化过程 为实例分配内存: 初始化附加成员,包括方法表指针和SyncBlockIndex变量(我们已经 ...
- java转换图片压缩生成webp格式
http://blog.csdn.net/xu_san_duo/article/details/79085718
- E - TOYS
来源 poj 2318 Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad ...
- G - Rabbit and Grass
大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛公园,不去和AC男约会,两个人竟然猫在寝食下棋-- 说是 ...
- ssh登录慢解决办法
这两天ssh登录局域网的一台服务器非常慢,严重影响工作效率,怎么办?查了一下网上的解决办法,总结一下: 使用命令ssh -v xxx@x.x.x.x 可以看到debug信息,找到问题出在哪: debu ...