[LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目
思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的。
思路2:第i天买出,能赚到的最大利润是多少呢?就是第i天的价格减去 0~ i-1天中最小的。
和前两道题比起来的话,这道题最难了,因为限制了交易次数。
解决问题的途径我想出来的是:既然最多只能完成两笔交易,而且交易之间没有重叠,那么就divide and conquer。
设i从0到n-1,那么针对每一个i,看看在prices的子序列[0,...,i][i,...,n-1]上分别取得的最大利润(第一题)即可。
这样初步一算,时间复杂度是O(n2)。
改进:
改进的方法就是动态规划了,那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。
第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。
所以最后算法的复杂度就是O(n)的。
/*
解释:
首先,因为能买2次(第一次的卖可以和第二次的买在同一时间),但第二次的买不能在第一次的卖左边。
所以维护2个表,f1和f2,size都和prices一样大。
意义:
f1[i]表示 -- 截止到i下标为止,左边所做交易能够达到最大profit;[0,...,i]的利润
f2[i]表示 -- 截止到i下标为止,右边所做交易能够达到最大profit;[i,...,n-1]的利润
那么,对于f1 + f2,寻求最大即可。
*/
对于f1[i],求解过程中用price[i] 减去之前的最小值 和 f1[i-1]做比较,取最大值
动态规划转移方程 f1[i] = max(f1[i-1], price[i]- min)
对于f2[i],求解过程中用后面的最大值减去price[i]和f2[i+1]做比较,取最大值
动态规划转移方程 f2[i] = max(f2[i+1], max-price[i])
思路解释完毕,上code:
minX[i] 表示0 到 i 的最小值 的price
max[i] 表示i到n-1的最大值的price
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() == )
return ;
vector<int> f1(prices.size());
vector<int> f2(prices.size());
vector<int> minX(prices.size());
vector<int> maxX(prices.size());
minX[] = prices[];
for(int i = ; i< prices.size();i++ )
{
minX[i] = min(minX[i-], prices[i]);
}
maxX[prices.size()-] = prices[prices.size()-];
for(int i = prices.size() -; i >=; i-- )
{
maxX[i] = max(maxX[i+], prices[i]);
}
f1[] = ;
for(int i = ; i< prices.size();i++ )
{
f1[i] = max(f1[i-],prices[i]-minX[i]);
}
f2[prices.size()-] = ;
for(int i = prices.size() -; i >=; i-- )
{
f2[i] = max(f2[i+],maxX[i]- prices[i]);
}
int sum = ;
for(int i = ; i< prices.size();i++ )
sum = max(sum, f1[i] + f2[i]);
return sum;
}
};
优化:可以对上述code稍微优化一下,maxX 和minX array 并不需要,只要保留一个变量mini和maxX即可,不过由于f1和f2 的存在,空间复杂度还是O(n)
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() == )
return ;
vector<int> f1(prices.size());
vector<int> f2(prices.size());
int mini = prices[];
f1[] = ;
for(int i = ; i< prices.size();i++ )
{
f1[i] = max(f1[i-],prices[i]-mini);
mini = min(mini, prices[i]);
}
int maxi = prices[prices.size()-];
f2[prices.size()-] = ;
for(int i = prices.size() -; i >=; i-- )
{
f2[i] = max(f2[i+],maxi - prices[i]);
maxi = max(maxi, prices[i]);
}
int sum = ;
for(int i = ; i< prices.size();i++ )
sum = max(sum, f1[i] + f2[i]);
return sum;
}
};
[LeetCode] Best Time to Buy and Sell Stock III的更多相关文章
- 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] 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: Best Time to Buy and Sell Stock III [123]
[称号] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- [leetcode]Best Time to Buy and Sell Stock III @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...
- leetcode -- Best Time to Buy and Sell Stock III TODO
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode——Best Time to Buy and Sell Stock III
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- LeetCode OJ--Best Time to Buy and Sell Stock III
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...
随机推荐
- OpenGL 4.3配置教程
OpenGL 4.3配置教程 下载开发包 需要下载的开发包主要包含如下几个组件:freeglut+glew+ OpenGL.Development.Cookbook+源码+GLM+SOIL. Open ...
- 为什么需要DTO(数据传输对象)
DTO即数据传输对象.之前不明白有些框架中为什么要专门定义DTO来绑定表现层中的数据,为什么不能直接用实体模型呢,有了DTO同时还要维护DTO与Model之间的映射关系,多麻烦. 然后看了这篇文章中的 ...
- ASP.NET MVC学习系列(二)-WebAPI请求(转)
转自:http://www.cnblogs.com/babycool/p/3922738.html 继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的g ...
- MVC3学习:利用mvc3+ajax实现登录
用到的工具或技术:vs2010,EF code first,JQuery ajax,mvc3. 第一步:准备数据库. 利用EF code first,先写实体类,然后根据实体类自动创建数据库:或者先创 ...
- Matlab中的fread函数
Matlab中fread函数用法 "fread"以二进制形式,从文件读出数据. 语法1:[a,count]=fread(fid,size,precision) 语法2:[a, ...
- [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构
10.2 How would you design the data structures for a very large social network like Facebook or Linke ...
- LeetCode-Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 熟悉linux开发环境(实验)
北京电子科技学院(BESTI) 实验报告 课程: 深入理解计算机系统 班级: 1353班 姓名:张若嘉 杨舒雯 学号:20135330 20135324 成绩: 指导教师:娄嘉鹏 实验日期:2015. ...
- 查询一个ID出现2种结果的情况
项目中书籍分个人和机构,分属不同的表 所以有的时候ID是一样的,那么只根据ID查询书籍就会存在ID=xxx的既有个人又有机构,而通常我们可能只需要一个,多的没做区分就出问题了! 所以数据统一做查询的时 ...
- NABC竞争性需求分析
设计一个五子棋游戏 下面是比较系统的框架-NABC模型 1) N (Need 需求) 现在随着人们的生活越来越好,电脑已经成为每家每户的必备品了,而且很多人工作的地方都也是必备的电脑,而 ...