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

class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()==0)return 0;
int n=prices.size();
vector<int> left(n);
vector<int> right(n);
int min=prices[0];
int max=prices[n-1];
int res=0;
for(int i=1;i<n;i++)
{
min=min<prices[i]?min:prices[i];
left[i]=left[i-1]>(prices[i]-min)?left[i-1]:(prices[i]-min);
}
for(int j=n-2;j>=0;j--)
{
max=max>prices[j]?max:prices[j];
right[j]=right[j+1]>(max-prices[j])?right[j+1]:(max-prices[j]);
}
for(int i=0;i<n;i++)
{
res=res>(left[i]+right[i])?res:(left[i]+right[i]);
}
return res;
}
};

  

最多两次股票交易-Best Time to Buy and Sell Stock III的更多相关文章

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

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

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

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

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

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

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

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

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

随机推荐

  1. zabbix Lack of free swap space

    Zabbix初始设计是大型公司用于监控服务器集群的,但日常中也用于监控VPS或云主机.后者情况下Zabbix的很多配置和属性就没有经过优化,取决于监控的对象和用途,经常需要对一些Zabbix配置进行调 ...

  2. C# 经典入门15章-TextBoxControl

    第一步:设计界面如下:

  3. 用css怎么制作下拉列表

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. XML字符串解析成对象的时候应注意空格

    BomList bomList=(BomList)unmarshaller_bom.unmarshal(new StringReader(xml));xml 不能以空格开头

  5. (转)StringTokenizer类的使用

    StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数.1.构造函数public StringTokenizer(String str)public String ...

  6. 我有一壶酒 Android学习之Service(1)--->BinderService方式

    本文只讨论扩展Binder类 创建一个Binder.xml <?xml version="1.0" encoding="utf-8"?> <L ...

  7. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

  8. 关于css的伪类和伪元素

    现在才发现自己一直没有分清楚css的伪类和伪元素啊,so,总结一下. CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相 ...

  9. strace 分析 跟踪 进程错误

    strace是什么? 按照strace官网的描述, strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进程状态变更等. ...

  10. springMVC源码下载地址

    https://github.com/spring-projects/spring-framework/tags可以选择需要的版本进行下载.