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. 综合经验:IO读写错误必然导致程序崩溃

    仿佛是忽然间产生的问题,每次程序退出时候,必然崩溃,花了整整一天才找到原因,就是对数据库的IO读写错误.主要是因为析构函数调用了Disconnect函数,内容如下: void SFTPTool::Di ...

  2. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...

  3. linux 匹配查询列表中包含某一特殊字符的所有行中的某一列

    命令: ll | grep sh | awk '{print $9}' 解析: 其中,匹配列的命令为awk '{print $n}',$n为匹配的第几列.

  4. Android SeekBar实现音量调节

    SeekBar可以通过滑块的位置来标识数值----而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等. SeekBar允许用户改变拖动条的滑块外观,改变滑块 ...

  5. 一个Redis实现的分布式锁

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.conne ...

  6. 【HDOJ】1263 水果

    hash,使用stl map ac.学了find_if等强大的东西,第一次使用stl模板. #include <iostream> #include <cstdio> #inc ...

  7. MVC——母版与分部

    背景: 母版是因为有一些网站里的很多网页都是采用相同的布局,所以只需要写一个母版,然后在母版该写不同模板的地方加上@RenderBody(),然后创建不同模块的时候只需要创建视图,然后选择母版就可以了 ...

  8. Nagios‘process_cgivars()’函数差一错误拒绝服务漏洞

    漏洞名称: Nagios‘process_cgivars()’函数差一错误拒绝服务漏洞 CNNVD编号: CNNVD-201312-495 发布时间: 2013-12-27 更新时间: 2013-12 ...

  9. json-lib反序列化时(JSONObject.toBean),时间类型为空的处理

    需求: 在我们的项目里希望JsonString传入日期类型值为空时,JSONObject.toBean时可以将Java对象的该日期属性设为null. 解决过程: json-lib反序列化Json字符串 ...

  10. 第K顺序统计量

    1.第K顺序统计量概念 在一个由n个元素组成的集合中,第k个顺序统计量是该集合中第k小的元素.例如,最小值是第1顺序统计量,最大值是第n顺序统计量. 2.求Top K元素与求第K顺序统计量不同 Top ...