最多两次股票交易-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).
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的更多相关文章
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- [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 ...
随机推荐
- svn up 提示:Skipped '.'
>svn up Skipped '.' >svn cleanup '.' is not a working copy directory >svn co https://192.16 ...
- kill -QUIT <pid>
On Solaris and Linux a thread dump is also printed if the J2SE process receives a QUIT signal. So ki ...
- 详细讲解MOSFET管驱动电路(转)
作者: 来源:电源网 关键字:MOSFET 结构 开关 驱动电路 在使用MOS管设计开关电源或者马达驱动电路的时候,大部分人都会考虑MOS的导通电阻,最大电压等,最大电流等,也有很多人仅仅考虑这些 ...
- Why attitude is more important than IQ
原文:http://www.businessinsider.com/why-attitude-is-more-important-than-iq-2015-9?IR=T& LinkedIn I ...
- 设置div,td失去焦点
$(downobj).attr({"tabindex":"0"});还需要设置css样式outline:none;
- [iOS]C语言技术视频-16-指针变量高级用法(堆栈内存)
下载地址: 链接: http://pan.baidu.com/s/1qWqWnGo 密码: igjc
- 高橋君とカード / Tak and Cards
高橋君とカード / Tak and Cards Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Score : 300 p ...
- angularJS在本机运行时的注意事项
其实要讲的就只有一个,那就是给浏览器加上本地Ajax运行,当你使用到了某些angularJS的功能的时候,例如路由,你直接运行页面打开之后你会发现是空白的,打开控制台 发现 XMLHttpReques ...
- cnn 文章
http://www.cnblogs.com/fengfenggirl/p/cnn_implement.html http://www.2cto.com/kf/201603/493553.html h ...
- php解决微信开发中用户昵称中的特殊字符与emoji表情写入mysql错误的问题
解决办法:将3个字节的特殊字符与emoji表情替换掉即可. $nickname = preg_replace('/xE0[x80-x9F][x80-xBF]'.'|xED[xA0-xBF][x80-x ...