LeetCode 笔记23 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III
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 two transactions.
现在A股涨这么好,要是股票都提前知道价格就好了(@_@)
首先,考虑如果至多只能做一次交易呢?
那很简单嘛,大家都知道O(n^2)的方法,两个循环,确定(buyDay, sellDay)取到最大的profit。
怎么优化呢?
其实如果只做一次交易,那么遍历一次数组就可以了。
从0开始,到最后一天。在遍历到第i天的时候,我们用prices[i]减去当前所知道的最小的price,看一下是否比当前max profit大,如果是,就更新max profit。
我们不用关心后面的更小值哦,因为它们不影响当前profit,毕竟你只有先买了才能卖。

同理也可以从最后一天开始往前遍历,这时候我们不记录当前所知道的最小price,而是最大值,用最大值减去prices[i]来和max profit比较。代码在这下面。
public int maxProfit(int[] prices) {
if (prices.length <= 1) {
return 0;
}
int maxProfit = 0;
int minIndex = 0;
for(int i = 1; i < prices.length; i++) {
if (prices[i] < prices[minIndex]) {
minIndex = i;
}
if (prices[i] - prices[minIndex] > maxProfit) {
maxProfit = prices[i] - prices[minIndex];
}
}
return maxProfit;
}
然后我们来看如何计算”买卖两次“的最大profit。
因为有了买卖一次的交易算法,我们比较容易去这样想。把整个个prices数组分成两部分,计算前一部分买卖一次的最大值,计算后一部分买卖的最大值,然后求和。然后从0到length重复该操作,求出整个数组上买卖两次的最大值。
不过这样复杂度变成了O(n^2)。
有没有更好的方法呢?
其实这样想,我们在计算买卖一次的遍历过程中,已经有这样的信息了,那就是,在第0天到第i天,买卖一次能得到的最大profit,假设是forward[i]。同理,如果从后面往前遍历的过程中,我们拿到从第i天到最后一天,买卖一次能得到的最大profit,假设是backward[i]。
于是我们最后一步要求的不就是max(forward[i] + backward[i] for i = 0... length)嘛?这样O(n)就能求出来了。
代码如下:
public int maxProfit(int[] prices) {
int[] maxProfit = new int[prices.length];
if (prices.length <= 1) {
return 0;
}
//forward
int minIndex = 0;
for(int i = 1; i < prices.length; i++) {
if (prices[i] < prices[minIndex]) {
minIndex = i;
}
maxProfit[i] = Math.max(maxProfit[i], prices[i] - prices[minIndex]);
}
//backward
int maxIndex = prices.length - 1;
int ret = 0;
int iMax = 0;
for(int i = prices.length - 2; i >= 0; i--) {
if (prices[i] > prices[maxIndex]) {
maxIndex = i;
}
iMax = Math.max(iMax, prices[maxIndex] - prices[i]);
ret = Math.max(ret, iMax + maxProfit[i]);
}
return ret;
}
注意我们没有使用backward[i],因为第二次遍历直接就能得到max profit了。
LeetCode 笔记23 Best Time to Buy and Sell Stock III的更多相关文章
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock III
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...
- LeetCode OJ 123. Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode刷题笔记】Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- leetcode第一刷_Best Time to Buy and Sell Stock III
这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类.只是相比之下稚嫩多了.有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律.这道题 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
随机推荐
- <极客学院>视频教程学习笔记-iOS中CALayer的使用
<1>CALayer简介 1.CALayer一般作为UIView的容器而使用. 2.CALayer是一个管理者图片载体(image-based content)的层结构 3.直接修改单独创 ...
- [PL/SQL工具]绿色版PLSQL工具登录时提示初始化失败,无法锁定OCI.dll错误
问题现象:使用绿色版PL/SQL工具进行登录时报如下截图错误: 问题描述:初始化失败,无法锁定oci.dll 解决方法:在PLSQL的菜单栏里依次选择 工具->首选项,在OCI库(自动检测为空) ...
- nginx设置反向代理后,页面上的js css文件无法加载
问题现象: nginx配置反向代理后,网页可以正常访问,但是页面上的js css文件无法加载,页面样式乱了. (1)nginx配置如下: (2)域名访问:js css文件无法加载: (3)IP访问:j ...
- 深入理解spring中的各种注解
Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@ ...
- 问题解决——warning C4503 超出修饰名的长度,名称被截断
========================声明============================ 本文原创,转载请注明作者和出处,并保证文章的完整性(包括本声明). 本文不定期修改完善,为 ...
- MyEclipse10 离线图文安装SVN插件教程
一.下载SVN插件subclipse 1.下载 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 ...
- hdu 2255 奔小康赚大钱--KM算法模板
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 题意:有N个人跟N个房子,每个人跟房子都有一定的距离,现在要让这N个人全部回到N个房子里面去,要 ...
- pageX和pageY
pageX() 属性是鼠标指针的位置,相对于文档的左边缘. pageY() 属性是鼠标指针的位置,相对于文档的上边缘. 例1 $(document).mousemove(function(e){ $( ...
- cut
cut是一个针对行的数据选取命令 SYNOPSIS cut [OPTION]... [FILE]... OPTION -b 以字节为单位进行分割,如果是多字节的话就需要注意了 -c 以字符为单位进行分 ...
- windows下 MySQL数据库双向同步 配置步骤
最近在项目中遇到了要实现服务器上MySql数据双向同步,在网上找了很多资料,但是大部分都是在liux系统下配置的, 而且都是互相转载,没有一个详细的步骤,于是决定写一个windows系统下 ...