题目

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. Net Core开源日志框架

    Net Core开源日志框架 Exceptionless - .Net Core开源日志框架 作者:markjiang7m2原文地址:https://www.cnblogs.com/markjiang ...

  2. Ubuntu常用指令集

    Ubuntu Linux 操作系统常用命令详细介绍 ( 1)Udo apt-get install 软件名 安装软件命令 sudo nautilus 打开文件(有 root 权限)su root 切换 ...

  3. webpack.config.js====output出口文件的配置

    output: { filename: './js/[name].[hash:8].js', /* * filename:在使用webpack-dev-server模式时,如果要使用hash,是不可以 ...

  4. Redis key 键

    1.set key value    //设置.修改值 2.get key  //如果key不存在,返回nil,表示空. 3.type key   //返回key对应的value的数据类型 4.ren ...

  5. 图解HTTP总结

    一.TCP/IP 的分层管理 二.TCP/IP通信传输流 ARP地址解析协议参考:https://www.cnblogs.com/csguo/p/7527303.html 三.各种协议与HTTP协议的 ...

  6. 个人博客 attack.cf

    新开了个emlog搭的博客 地址:attack.cf 主要分享一下网络安全方面的东西和一些精品资源 欢迎来访

  7. htmlparse

    <html>    <head>        <style>                textarea{                width:800p ...

  8. jsp-简单的猜数小游戏

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  9. jquery解析xml,获取xml标签名

    先给一个简单的XML,结构如下 <?xml version="1.0" encoding="uft-8" ?> <msg> <ro ...

  10. 【Python图像特征的音乐序列生成】解析ABC格式的文件(修改版)

    ABC格式,是一个音乐标准,ABC Plus Project最新的标准是2.x. ABC格式的音乐长成这样: X: T:Abacus % Nottingham Music Database S:By ...