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.
解题思路:
https://leetcode.com/discuss/18330/is-it-best-solution-with-o-n-o-1%E3%80%82
本题是Best Time to Buy and Sell Stock系列最难的一道,
参考Java for LeetCode 123 Best Time to Buy and Sell Stock III解法二的思路,四个数组可以压缩为两个数组,JAVA实现如下:
public int maxProfit(int k, int[] prices) {
if (k == 0 || prices.length < 2)
return 0;
if (k > prices.length / 2) {
int maxProfitII = 0;
for (int i = 1; i < prices.length; ++i)
if (prices[i] > prices[i - 1])
maxProfitII += prices[i] - prices[i - 1];
return maxProfitII;
}
int[] buy=new int[k];
int[] sell=new int[k];
for(int i=0;i<buy.length;i++)
buy[i]=Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++)
for (int j = k - 1; j >= 0; j--) {
sell[j] = Math.max(sell[j], buy[j] + prices[i]);
if (j == 0)
buy[j] = Math.max(buy[j], -prices[i]);
else
buy[j] = Math.max(buy[j], sell[j - 1] - prices[i]);
}
return sell[k - 1];
}
Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】的更多相关文章
-
Java for LeetCode 123 Best Time to Buy and Sell Stock III【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】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
-
Java for LeetCode 121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
-
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 ...
-
Java for LeetCode 122 Best Time to Buy and Sell Stock II
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 ...
随机推荐
-
深入MySQL索引
MySQL索引作为数据库优化的常用手段之一在项目优化中经常会被用到, 但是如何建立高效索引,有效的使用索引以及索引优化的背后到底是什么原理?这次我们深入数据库索引,从索引的数据结构开始说起. 索引原理 ...
-
Socket网络编程--FTP客户端
Socket网络编程--FTP客户端(1)(Windows) 已经好久没有写过博客进行分享了.具体原因,在以后说. 这几天在了解FTP协议,准备任务是写一个FTP客户端程序.直接上干货了. 0.了解F ...
-
WordCount示例深度学习MapReduce过程(1)
我们都安装完Hadoop之后,按照一些案例先要跑一个WourdCount程序,来测试Hadoop安装是否成功.在终端中用命令创建一个文件夹,简单的向两个文件中各写入一段话,然后运行Hadoop,Wou ...
-
LeetCode(84) Largest Rectangle in Histogram
题目 Given n non-negative integers representing the histogram’s bar height where the width of each bar ...
-
iframe中的各种跳转方法(转)
一.背景A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,在D中跳转页面的写法区别如下. 二.JS跳转window.location.href.locatio ...
-
mysql操作1
一.连接MYSQL.格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root - ...
-
机器学习实战笔记5(logistic回归)
1:简单概念描写叙述 如果如今有一些数据点,我们用一条直线对这些点进行拟合(改线称为最佳拟合直线),这个拟合过程就称为回归.训练分类器就是为了寻找最佳拟合參数,使用的是最优化算法. 基于sigmoid ...
-
在ASP.NET Core 中怎样使用 EF 框架读取数据库数据
添加测试数据 我们首先使用 SQLite Studio 添加三条数据 ID Name 1 李白 2 杜甫 3 白居易 使用 SQLite Studio 打开我们的 blogging.db 数据库,双击 ...
-
mysql表理解
4.1 索引组织表 1.在innodb存储引擎中,每张表都有个主键,如果在创建表时没有显式地定义主键,则innodb存储引擎会按如下方式选择或创建主键: ①:首先判断表中是否有非空的唯一索引,如果有, ...
-
span的title标签中的换行
var strs = data.flowSummary; strs=strs.replace(/燮r燮n/g," "); js的全局替换用/要替换的字符串/g span的titl ...