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. ural1772 Ski-Trails for Robots

    Ski-Trails for Robots Time limit: 1.0 secondMemory limit: 64 MB One of the stages of the Robot Cross ...

  2. Java关键字transient和volatile小结(转)

    Java关键字transient和volatile小结(转) transient和volatile两个关键字一个用于对象序列化,一个用于线程同步,都是Java中比较高阶的话题,简单总结一下. tran ...

  3. 判断android文件是否加壳

    判断android文件是否加壳 查看文件是否有多个进程 反编译文件class.dex,看文件结构 查看文件特征,libsecexe libsecmain等 反编译so文件,看函数是否加密

  4. RPC远程过程调用机制底层原理

  5. 【HighCharts系列教程】四、颜色属性——colors

    一.Colors属性说明 配置Colors,可以自定义数据列的颜色. 默认下colors就包含一系列颜色,在个性化或需要调整颜色的顺序下,我们可以配置该属性. 二.colors属性详解 Colors属 ...

  6. 高橋君とカード / Tak and Cards

    高橋君とカード / Tak and Cards Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 300 p ...

  7. CodeForces 605A Sorting Railway Cars

    求一下最长数字连续上升的子序列长度,n-长度就是答案 O(n)可以出解,dp[i]=dp[i-1]+1,然后找到dp数组最大的值. #include<cstdio> #include< ...

  8. PAT 天梯赛 L2-005 集合相似度

    set的应用 题目链接 题解 有点像集合的交并操作,直接利用set进行处理,因为set有去重的功能,而且set是利用红黑树实现的,查找速度快O(logN). 代码如下: #include<cst ...

  9. JS中判定问题

    今天项目遇到一个很奇怪的问题,具体问题不好说明简化下,有如下一个方法: function editStu(index, rowData) { if (index == "") { ...

  10. 移动web开发资源大整合

    移动web开发资源大整合 http://www.cnblogs.com/PeunZhang/p/3407453.html