Best Time to Buy and Sell Stock III 买卖股票的最好时间
来源:互联网 发布:设计淘宝店要多少钱 编辑:程序博客网 时间:2024/04/28 09:40
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n=prices.size();
if(n<2)
return 0;
vector<int> left(n,0);
vector<int> right(n,0);
int minbuy=prices[0];
left[0]=0;
for(int i=1;i<n;i++)
{
left[i]=max(left[i-1],prices[i]-minbuy);
minbuy=min(minbuy,prices[i]);
}
int maxsell=prices[n-1];
right[n-1]=0;
for(int i=n-2;i>=0;i--)
{
right[i]=max(right[i+1],maxsell-prices[i]);
maxsell=max(maxsell,prices[i]);
}
int maxprofit=0;
for(int i=0;i<n;i++)
{
maxprofit=max(maxprofit,left[i]+right[i]);
}
return maxprofit;
}
};
public:
int maxProfit(vector<int> &prices) {
int n=prices.size();
if(n<2)
return 0;
vector<int> left(n,0);
vector<int> right(n,0);
int minbuy=prices[0];
left[0]=0;
for(int i=1;i<n;i++)
{
left[i]=max(left[i-1],prices[i]-minbuy);
minbuy=min(minbuy,prices[i]);
}
int maxsell=prices[n-1];
right[n-1]=0;
for(int i=n-2;i>=0;i--)
{
right[i]=max(right[i+1],maxsell-prices[i]);
maxsell=max(maxsell,prices[i]);
}
int maxprofit=0;
for(int i=0;i<n;i++)
{
maxprofit=max(maxprofit,left[i]+right[i]);
}
return maxprofit;
}
};
0 0
- Best Time to Buy and Sell Stock III 买卖股票的最好时间
- Best Time to Buy and Sell Stock 买卖股票最好的时间
- Best Time to Buy and Sell Stock II 买卖股票最好的时间II
- Best Time to Buy and Sell Stock 买卖股票的最好时间
- 【LintCode】 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
- LeetCode OJ 之 Best Time to Buy and Sell Stock III (买卖股票的最佳时间 - 三)
- Best Time to Buy and Sell Stock III 两次买卖股票的最佳时机 (重重重)
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- Best Time to Buy and Sell Stock 【最佳买卖股票的时间】【容易】
- LeetCode-121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)
- 122. Best Time to Buy and Sell Stock II 最佳买卖股票的时间
- 151.Best Time to Buy and Sell Stock III-买卖股票的最佳时机 III(中等题)
- Best Time to Buy and Sell Stock III 最佳时间买入卖出股票(最多两次买卖)@LeetCode
- Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益
- 121. Best Time to Buy and Sell Stock 最佳买卖股票时间
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- prepareAsync called in state 1
- Eclise中SVN插件安装和使用
- 【HDFS】datanode注册信息都有啥?
- 有趣的JavaScript原生数组函数
- cocos2dx游戏开发简单入门视频教程 (cocos2d-x)-第2天
- Best Time to Buy and Sell Stock III 买卖股票的最好时间
- ipcs
- Oracle中分区表的使用
- Tomcat6的服务器配置
- TreeSet
- 百度云存储教程---免费建立自己的静态站点
- Jni接口-深入研究参数的传递(一)
- js 多态性实例
- 与其投机比特币:不如挖掘大数据