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的更多相关文章

  1. 【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 ...

  2. 【刷题-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 ...

  3. 【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 ...

  4. 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 ...

  5. 【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 ...

  6. 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. leetcode第一刷_Best Time to Buy and Sell Stock III

    这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类.只是相比之下稚嫩多了.有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律.这道题 ...

  8. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  9. 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 ...

随机推荐

  1. 使用网站processon在线作图

    网站:https://www.processon.com/ 该网站提供多种图形的在线制作,并支持多人协作.   目前提供以下图形的制作: 个人管理界面:

  2. 人生导师——如何学习C++的Windows方向

    原来发在百度上,今天被人挖坟挖出来了,我就再贴出来吧. -----------------------有什么说的不对的地方---------------------- 本文原创,转载请注明出处并保持文 ...

  3. PL/SQL之--包

    一.包 包是一组相关过程.函数.常量.变量.游标.异常等PL/SQL程序设计元素的组合.它类似于C++和Java中的类,其中变量相当于类中的成员变量,过程和函数相当于类中的方法.通过使用包,可以使开发 ...

  4. chrome45以后的版本安装lodop后,仍提示未安装解决

    请先查看你chrome浏览器的版本,如果是45版本以前的版本,安装后仍提示 "未安装" 或 "请升级" 请参照本链接解决:http://blog.sina.co ...

  5. Hadoop Resource

    http://www.aiopass4sure.com/cloudera-exams/ccd-410-exam-questions/which-process-describes-the-lifecy ...

  6. 怎样快速学会ZBrush 中的移动笔刷的运用

    本篇视频教程,进入ZBrush®最精彩章节,雕刻前我们需要认识的雕刻工具-笔刷.zbrush自带了多种笔刷供大家选择和使用,掌握和用好这些笔刷将让我们的雕刻工作更加自由.本课的内容将主要向大家介绍最基 ...

  7. python中thread模块中join函数

    http://www.cnblogs.com/vingi/articles/2657790.html for i in range(10): t = ThreadTest(i) thread_arr. ...

  8. 两道相似KMP题

    1.POJ 3450 Coporate Identity 这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NUL ...

  9. 深入理解maven及应用--转

    (一):生命周期和插件 在项目里用了快一年的maven了,最近突然发现maven项目在eclipse中build时非常慢,因为经常用clean install命令来build项目,也没有管那么多,但最 ...

  10. map学习笔记

    collection是单列集合,map是双列集合.其中包含<k,v>键值对,注意:键具有唯一性,而值不唯一. 在此列举三个读取方式:keyset,valueset,及entryset. k ...