[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题使用动态 ...
随机推荐
- box-css3
父容器样式必须有定义:"{ display: -webkit-box }" 现象:水平时只能在一行布局,子容器在垂直方向上会填充父容器. 技巧:可以做水平居中和垂直居中.也可以实现 ...
- 如何“刷leetcode”
做题目的: 获得 offer 巩固算法与数据结构知识,将学到的东西用出来 如何做题: 根据章节与难度来做. 比如你学了 linked list,就去找到标签为 linked list 的题目,然后根据 ...
- 添加web引用和添加服务引用有什么区别?
添加web引用和添加服务引用有什么区别,Add Service References 和 Add Web References 有啥区别?参考 http://social.microsoft.com/ ...
- python数字图像处理(10):图像简单滤波
对图像进行滤波,可以有两种效果:一种是平滑滤波,用来抑制噪声:另一种是微分算子,可以用来检测边缘和特征提取. skimage库中通过filters模块进行滤波操作. 1.sobel算子 sobel算子 ...
- jquery-lazyload延迟加载图片 及 加载顺序 bug 修复
jquery-lazyload延迟加载图片 代码修改片段 function update() { var counter = 0; /**fix by weiyj start***/ elemen ...
- [CareerCup] 6.3 Water Jug 水罐问题
6.3 You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring ...
- 慢牛系列四:好玩的React Native
在上次随笔(系列三)中,我试着用RN实现了一个Demo,感觉很不错,当时遇到的问题这篇文章里基本都解决了,比如导航动画问题,这篇文章里主要介绍RN的动画,学会动画以后,各种小创意都可以实现了^^ 下面 ...
- javascript中的闭包,超简单论述,保证小学生必懂
js中的闭包已经有很多论断了,大家伙有没有听懂了,先引用一片比较高端 的 ”汤姆大叔“ 深入理解JavaScript系列(16):闭包(Closures) 好了,为了引起大家的兴趣,先来小诗一首 v ...
- 扫描二维码下载安装apk的app
将apk文件放到服务器上,下载链接直接生成二维码,用微信扫描时不能直接下载.页面只是刷新一下. 想实现微信扫描下载apk的app客户端,需要把下载链接做到一个网页上, 将网页生成一个二维码. 直接下载 ...
- ModelProxy 前端接口配置建模框架
ModelProxy 轻量级的接口配置建模框架(1) 先看一下这个博客说明为什么需要用ModelProxy的前端轻量级的框架吧: http://developer.51cto.com/art/ ...