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 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).
Subscribe to see which companies asked this question
分析
紧接着121 和122 的另外一道题目,此次要求只能进行两次买卖交易,求最大利润。
一篇很好的分析文章,参考博客。
第一步扫描,先计算出子序列[0,…,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。
计算方法也是利用第一个问题的计算方法。
第二步是逆向扫描,计算子序列[i,…,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。
第三步,求[0,i]的最大利润与[i,n-1]的最大利润之和的最大值,所以最后算法的复杂度就是O(n)的。
AC代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0;
int n = prices.size();
vector<int> profits(n, 0), profits_reverse(n,0);
//正向遍历,profits[i]表示 prices[0...i]内做一次交易的最大收益.
int low = prices[0] , cur_profit = 0;
for (int i = 1; i < n; ++i)
{
if (prices[i] < low)
{
low = prices[i];
}
else{
if (cur_profit < prices[i] - low)
cur_profit = prices[i] - low;
}
profits[i] = cur_profit;
}//for
//逆向遍历, profits_reverse[i]表示 prices[i...n-1]内做一次交易的最大收益.
//当前最大价格
int high = prices[n - 1];
cur_profit = 0;
for (int i = n - 2; i >= 0; --i)
{
if (prices[i] > high)
high = prices[i];
else{
if (cur_profit < high - prices[i])
cur_profit = high - prices[i];
}//else
profits_reverse[i] = cur_profit;
}
int max_profile = 0;
for (int i = 0; i < n; i++)
{
if ((profits[i] + profits_reverse[i]) > max_profile)
max_profile = profits[i] + profits_reverse[i];
}
return max_profile;
}
};
LeetCode(123) Best Time to Buy and Sell Stock III的更多相关文章
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(121) 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 we ...
- 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 ...
- 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】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]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 ...
- 【刷题-LeetCode】123 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 ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- 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 ...
随机推荐
- windows/Linux下设置ASP.Net Core开发环境并部署应用
10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用 创建和开发ASP.NET Core应用可以有二种方式:最简单的方式是通过Visual Studio 2017 ...
- Win10 插入耳机后没有声音,拔出后电脑有声音
- SSIS控件使用
1.转换控件: 2.执行SQL任务,返回一个值后,判断全量,还是增量?
- linux touch和vi建立的文件是什么文件类型的
都是acsii类型的文本文档,但是也可以建立其他格式的,比如vi newFile.c(c是c语言动态链接库格式)
- AngularJS(八):http服务
本文也同步发表在我的公众号“我的天空” http服务 之前我们的示例都是在本地获取模拟数据,在实际应用中,所有的项目都将不可避免的从后台获取数据,我们都是通过Ajax来实现与服务器的通信.在Angul ...
- IOS制作纯色背景
// 生成纯色背景图- (UIImage *)createPureColorImageWithColor:(UIColor *)color alpha:(CGFloat)alpha size:(CGS ...
- [中文翻译] ASP.NET 5 简介(Introducing ASP.NET 5,原作ScottGu 2015/2/23)
本文出处 [中文翻译] ASP.NET 5 简介(Introducing ASP.NET 5,原作ScottGu 2015/2/23) 这是我的文章备份 http://www.dotblogs.co ...
- phar打包项目压力对比测试
工具 http_load 测试url: http://api.test.chaoma.me/agent/ad/good_goods/query http://api.test.chaoma.me/ag ...
- FMDB浅析(思想)
http://www.cnblogs.com/OTgiraffe/p/5931800.html 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQLite的API进行了封装,加上了面向 ...
- tpcc-mysql运行结果解读
前言 首先我们需要知道tpcc-mysql是干什么的.TPC-C是专门针对联机交易处理系统(OLTP系统)的规范,一般情况下我们也把这类系统称为业务处理系统.tpcc-mysql是percona基于T ...