题目

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.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Subscribe to see which companies asked this question

分析

紧接着121122 的另外一道题目,此次要求只能进行两次买卖交易,求最大利润。

一篇很好的分析文章,参考博客

第一步扫描,先计算出子序列[0,…,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。

计算方法也是利用第一个问题的计算方法。

第二步是逆向扫描,计算子序列[i,…,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。

第三步,求[0,i]的最大利润与[i,n-1]的最大利润之和的最大值,所以最后算法的复杂度就是O(n)的。

AC代码

class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0; int n = prices.size(); vector<int> profits(n, 0), profits_reverse(n,0); //正向遍历,profits[i]表示 prices[0...i]内做一次交易的最大收益.
int low = prices[0] , cur_profit = 0;
for (int i = 1; i < n; ++i)
{
if (prices[i] < low)
{
low = prices[i];
}
else{
if (cur_profit < prices[i] - low)
cur_profit = prices[i] - low;
}
profits[i] = cur_profit;
}//for //逆向遍历, profits_reverse[i]表示 prices[i...n-1]内做一次交易的最大收益.
//当前最大价格
int high = prices[n - 1];
cur_profit = 0;
for (int i = n - 2; i >= 0; --i)
{
if (prices[i] > high)
high = prices[i];
else{
if (cur_profit < high - prices[i])
cur_profit = high - prices[i];
}//else
profits_reverse[i] = cur_profit;
} int max_profile = 0;
for (int i = 0; i < n; i++)
{
if ((profits[i] + profits_reverse[i]) > max_profile)
max_profile = profits[i] + profits_reverse[i];
}
return max_profile;
}
};

GitHub测试程序源码

LeetCode(123) Best Time to Buy and Sell Stock III的更多相关文章

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

  2. 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 we ...

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

  4. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

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

  6. [leetcode]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 ...

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

  8. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

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

随机推荐

  1. selenum autoit上传图片

    目前,一般实现文件图片上传的方式都是有一个按钮,点击之后直接调用操作系统自身的弹框,选择文件后,实现上传.因为Selenium不支持调用操作系统的操作,所以这种情况下,利用Selenium无法完成图片 ...

  2. 数据结构之Hyperloglog

    前置知识 调和平均数 通常我们求一堆数的平均数 就是把一堆数加起来除以这堆数的数量,如 x1, x2, x3, x4, .... ,xn的平均数 H = (x1 + x2 + x3 + x4 + xn ...

  3. WebStorm技巧-常用快捷键

      Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*-*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 Ctrl+G 查找行 Ctrl+Shift+ ...

  4. Spring中统一相同版本的api请求路径的一些思考

    Spring中统一相同版本的api请求路径的一些思考 问题场景 当我们在实际开发中,可能会遇到开发相同同版本的api, 假设相同版本的api请求路径为/v1/functionA,/v1/functio ...

  5. IO(Properties、序列化流、打印流、CommonsIO)

    第1章 Properties类 1.1 Properties类介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字 ...

  6. SQL SERVER之填充因子

    建SQL SERVER索引的时候有一个选项,即Fillfactor(填充因子). 这个可能很少人会去注意它,但它也是比较重要的.大家可能也都知道有这个东西,但是如何去使用它,可能会比较迷糊.另外,即使 ...

  7. python基础教程总结15——1.即时标记

    1. 测试文档: # test_input.txt Welcome to World Wide Spam. Inc. These are the corporate web pages of *Wor ...

  8. 3. Netbackup 7.6客户端的安装(windows/linux)

    1 客户端的安装 1.1 Windows客户端安装 1.1.1 客户端hosts修改 windows xp/2003/vista/2008/7/8用户HOSTS文件是在“c:\windows\syst ...

  9. 干净卸载 Cloudera CDH 5 beta2

    Cloudera 的官方介绍: http://www.cloudera.com/content/cloudera-content/cloudera-docs/CM4Ent/4.8.1/Cloudera ...

  10. CPP-网络/通信:gsoap 的教程和使用

    1.1.1     gSOAP 1.1.1 .1      简介 gSOAP 编译工具提供了一个 SOAP/XML 关于 C/C++ 语言的实现,从而让 C/C++ 语言研发 web 服务或客户端程式 ...