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

1:特殊情况。2:从前到后和从后到前遍历两次数字;3:在遍历的过程中,保存以当前索引为结束点或者開始点的仅仅进行一次买卖的最大收益。4:注意边界结束条件

        int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int size = (int)prices.size();
vector<int> leftProfit(size, 0);
leftProfit[0] = 0;
int minValue = prices[0]; int result = 0; for(int i = 1; i < size; i++)
{
if(prices[i] > minValue)
{
leftProfit[i] = (prices[i] - minValue > leftProfit[i-1] ? prices[i] - minValue : leftProfit[i-1]);
}
else
{
minValue = prices[i];
leftProfit[i] = leftProfit[i-1];
}
} result = leftProfit[size-1] > leftProfit[size-2] ? leftProfit[size-2] : leftProfit[size-1];
int maxValue = prices[size -1];
int rightMaxProfit = 0; for(int i = size - 2; i >= 0; i--)
{
if(prices[i] < maxValue)
{
rightMaxProfit = (rightMaxProfit > maxValue - prices[i] ? rightMaxProfit : maxValue - prices[i]);
}
else
{
maxValue = prices[i];
} if(i == 0)
{
result = (result > rightMaxProfit ? result : rightMaxProfit);
}
else
{
result = (result > rightMaxProfit + leftProfit[i-1] ? result : rightMaxProfit + leftProfit[i-1]);
}
} return result;
}

111_leetcode_Best Time to Buy and Sell Stock III的更多相关文章

  1. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

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

  3. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

  4. 【leetcode】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 ...

  5. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    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

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  7. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

  8. [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 ...

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

随机推荐

  1. win7安装IIS及将网站发布到IIS上

    1. WIN7安装IIS:  控制面板----程序和功能-----打开或关闭windows功能,如图 展开Internet信息服务,按照下图方式进行选择,然后单击"确定",等待几分 ...

  2. ARM的NEON协处理器是什么

    ARM的NEON协处理器是什么 何谓多媒体扩展指令集?由于原理复杂坚涩,小编就简单的打个比方:厂商们分析平时处理器干哪些事情最慢.又最经常用到,然后把这些最消耗时间的事情固化成电路,做成一个额外的部分 ...

  3. 程序不稳定是因为C++基础不扎实

    最近开发的程序,逻辑上都实现了,但是感觉运行不稳定,程序时不时崩溃(不是逻辑运行不正确),至少找出2个错误: 情况1:char* szRemoteReal = new char[MAX_LENGTH] ...

  4. 如何使用GetManifestResourceStream

    在做开发时想要从程序集中加载一个xml文件数据,可是在运行后确取不到值,值始终是null. 代码如下: Stream ss = this.GetType().Assembly.GetManifestR ...

  5. 【HDOJ】2430 Beans

    这题目用线段树超时了,其实也差不多应该超时.10^6大数据量.看了一下网上的解法是单调队列.大概了解了一下,是个挺有意思的数据结构.首先,需要求满足0<=(S[r]-S[l])%p<=k时 ...

  6. poj3140Contestants Division

    链接 这叫树形DP吗..?断开某条边 求剩下两颗树数权值和的差最小 dfs一遍 枚举边 查了n久 wa n次  dp数组没初始化.. 在poj上1A感觉应该挺爽 #include <iostre ...

  7. Linux kernel ‘lbs_debugfs_write’函数数字错误漏洞

    漏洞名称: Linux kernel ‘lbs_debugfs_write’函数数字错误漏洞 CNNVD编号: CNNVD-201311-421 发布时间: 2013-11-29 更新时间: 2013 ...

  8. 如何从Win7中提取制作Windows PE3.0

    在D盘新建文件夹winpe,在winpe中新建sources.pe3和new文件夹,把附件中提供的工具imagex连文件夹一起放到winpe目录中. 制作方法: 1.把windows7光盘(或光盘镜像 ...

  9. apache开源项目--Lens

    Lens 提供了一个统一数据分析接口.通过提供一个跨多个数据存储的单一视图来实现数据分析任务切分,同时优化了执行的环境.无缝的集成 Hadoop 实现类似传统数据仓库的功能. 该项目主要特性: 简单元 ...

  10. [转]"由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断"的解决方案

    先使用如下命令登录到服务器: mstsc /v:{服务器IP} /admin 然后再使用下列方法之一即可. 方法一: 1.单击“开始→运行”,输入“gpedit.msc”打开组策略编辑器窗口,依次定位 ...