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.

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

 
设dpBack[i]为从第一天到第i天的最大的收益
设dpAfter[j]为从第j天到最后一天的最大收益
 
如何求最大收益可以考虑采用Best Time to Buy and Sell Stock I的方法
 
注意是最多买两次,也可以只买一次
 
 class Solution {
public:
int maxProfit(vector<int> &prices) { int n=prices.size();
if(n==)return ; vector<int> dpBack(n),dpAfter(n); int maxProfit=;
dpBack[]=;
int left=prices[]; for(int i=;i<n;i++)
{
if(prices[i]>left)
{
if(maxProfit<prices[i]-left) maxProfit=prices[i]-left;
}
else
{
left=prices[i];
} dpBack[i]=maxProfit;
} maxProfit=;
dpAfter[n-]=;
int right=prices[n-]; for(int j=n-;j>=;j--)
{
if(prices[j]<right)
{
if(maxProfit<right-prices[j]) maxProfit=right-prices[j];
}
else
{
right=prices[j];
} dpAfter[j]=maxProfit;
} int result=;
for(int i=;i<n-;i++)
{
if(dpBack[i]+dpAfter[i+]>result) result=dpBack[i]+dpAfter[i+]; if(result<dpBack[i+]) result=dpBack[i+];
} return result;
}
};

【leetcode】Best Time to Buy and Sell Stock III的更多相关文章

  1. 【LeetCode】Best Time to Buy and Sell Stock IV

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

  2. 【leetcode】Best Time to Buy and Sell Stock II

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

  3. 【leetcode】121-Best Time to Buy and Sell Stock

    problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...

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

  5. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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 最佳炒股时机之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

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

  9. 【数组】Best Time to Buy and Sell Stock I/II

    Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...

随机推荐

  1. Servlet Study 1

    this content below are come from the  JSR154 by sun Just for record purpose. if this relate to some ...

  2. WEB版一次选择多个文件进行批量上传(Plupload)的解决方案

    WEB版一次选择多个文件进行批量上传(Plupload)的解决方案  转载自http://www.cnblogs.com/chillsrc/archive/2013/01/30/2883648.htm ...

  3. (转)google Java编程风格中文版

    转:http://www.hawstein.com/posts/google-java-style.html 目录 前言 源文件基础 源文件结构 格式 命名约定 编程实践 Javadoc 后记 前言 ...

  4. 深入浅出Redis04使用Redis数据库(lists类型)

    一  lists类型及操作 List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是sring类 ...

  5. POJ3744Scout YYF I(求概率 + 矩阵快速幂)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6757   Accepted: 1960 Descr ...

  6. std::stringstream

    使用 std::stringstream,小心 内存! 适时 清空 缓冲 …… 2007年12月14日 星期五 : stringstream是个好东西,网上有不少文章,讨论如何用它实现各种数据类型的转 ...

  7. JS 下拉菜单

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  8. golang社工库数据扫描程序

    https://github.com/xiaojiong/scanfile 演示站点: http://www.weigongkai.com/   7G数据 2s完成扫描 package scanfil ...

  9. 用80x86汇编语言编程:1 + 2 + 3 + 4 + 5 + …… + n,和小于100,在屏幕上显示次数和结果。

    ;============================================== ;1+...+n < 100 ;--------------------------------- ...

  10. WPF 检测计算机网络连接情况

    ; ; ; ; [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(ou ...