【Best Time to Buy and Sell Stock III 】cpp
题目:
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.empty()) return 0;const int len = prices.size();vector<int> l(len,0),r(len,0);// from left to rightl[0] = 0;int l_min = prices[0];for ( int i = 1; i < l.size(); ++i ){l_min = std::min(l_min, prices[i]);l[i] = std::max(l[i-1], prices[i]-l_min);}// from right to leftr[len-1] = 0;int r_max = prices[len-1];for ( int i = len-1; i >= 0; --i ){r_max = std::max(r_max, prices[i]);r[i] = std::max(r[i-1], r_max-prices[i]);}// travseral the best two timesint max_profit = 0;for ( int i = 0; i < prices.size(); ++i ){ max_profit = std::max(max_profit, l[i]+r[i]);}return max_profit;} };
tips:
此题说最多交易两次,求最大获利。
直觉的想法就是,把整个时间段分割成两部分( 共有prices.size()种分类方法 );分好后分别求两部分各自的最大值;这种算法是O(n²)时间复杂度的。
模仿之前求过的largest rectangle in histogram这道题的思路,能否利用dp思想,把算过的中间结果都存起来,把时间复杂度降低到O(n)。
想到这个思路就比较明确了:
1. 从左向右走一遍,l[i]存放0~i最多交易1次获利最大的值
2. 从右向左走一遍,r[i]存放i~prices.size()-1最多交易一次获利的最大值
3. 遍历数组l和数组r,通过遍历每种分割情况下的最大获利,并最终获得最终的最大获利值。
这里还有个细节可能会产生疑义:如果以第i天作为分割点,那么这第i天是算到前半截还是后半截呢?
这里分两种情况:
1. 如果“前半截的最大利润”和“后半截的最大利润”只有一截涉及到了第i天,显然l[i]+r[i]这个算法是没问题的
2. 如果“前”、“后”两截都涉及到了第i天呢?这时候有两种理解方法:
2.1 前后交易两次:前半截的某一天买入,第i天卖了,挣一笔;第i天卖完又买入了,到后面的某一天又卖了,挣第二笔。两笔加起来最大。
2.2 前后交易一次:前半截的某一天买入,第i天虽然卖了获利最大,但是不卖,留着;等到后面的某一天发现获利最大,直接挣一笔最大的,同样获利最大。
因此,无论按照哪种理解方法,l[i]+r[i]都是合理的,不会因为第i天作为分割点而产生影响。
=====================================================
第二次过这道题,思路上有个地方没有理清(红字),在当天可以选择交易或者不交易,如果不交易那么等于左边(或右边)相邻元素的值。
class Solution { public:int maxProfit(vector<int>& prices) {if ( prices.empty() ) return 0;vector<int> l(prices.size(), 0);vector<int> r(prices.size(), 0);// lint minPrices = prices[0];for ( int i=1; i<prices.size(); ++i ){minPrices = min(prices[i], minPrices);l[i] = max(l[i-1], prices[i]-minPrices);}// rint maxPrices = prices[prices.size()-1];for ( int i=prices.size()-2; i>=0; --i ){maxPrices = max(maxPrices, prices[i]);r[i] = max(r[i+1], maxPrices - prices[i]);}// l to rint ret = 0;for ( int i=0; i<prices.size(); ++i ){ret = max(ret, l[i]+r[i]);}return ret;} };
转载于:https://www.cnblogs.com/xbf9xbf/p/4546359.html
【Best Time to Buy and Sell Stock III 】cpp相关推荐
- 【Best Time to Buy and Sell Stock II】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III
LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...
- Leetcode Best Time to Buy and Sell Stock III
Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...
- [LeetCOde][Java] 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 a ...
- Leetcode_123_Best Time to Buy and Sell Stock III
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43740415 Say you have an array ...
- Best Time to Buy and Sell Stock III
解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ...
- 【Leetcode122 -买股票的最佳时机 II Best Time to Buy and Sell Stock II】(C语言)
目录 买股票的最佳时机II 测试单元 题目分析 标准版 巧妙版(简称投机取巧版) 买股票的最佳时机II 给定一个数组,它的第i个元素是一支给定股票第i天的价格. 设计一个算法来计算你所 ...
- 最多两次股票交易-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 OJ - 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 a ...
最新文章
- php5.3.28安装,安装php-5.3.28 时问题
- 自动驾驶激荡风云录:来自圈内人的冷眼解读
- java新特性对数组的支持
- Linux centos7 Linux网络相关、firewalld和netfilter、netfilter5表5链介绍、iptables语法
- leetcode287. Find the Duplicate Number
- 关于server 2008 r2中文版安装多国语言用户界面包(英文)
- day5 python学习
- boost::thread_group相关的测试程序
- Rabbitmq的原生javaAPI讲解
- 顺序不能改变的算子,是否跟时间有关
- CSS居中对齐的各种方式
- 国有数据要素市场的政策红利,你get 到了吗?
- OpenCV调用摄像头录像并保存下来
- 工具篇 之 iTerm 2 用户名修改(基于 iTerm 2 + oh-my-zsh)
- 【pcre 依赖】linux安装pcre 依赖
- win10html5无法播放,Win10网页视频无法播放提示启用adobe flash player
- 人们对人工智能的看法(积极篇)
- 什么是归一化,它与标准化的区别是什么?
- 形式语言与自动机基础
- Booktrack Classroom(有声电子书)
热门文章
- 通过手动抛自定义异常实现spring事务回滚
- 解决matplotlib中文显示问题
- 3元购买微信小程序解决方案一个月
- tomcat启动后,页面浏览时报错 Unable to compile class for JSP的解决方案
- 如何确定类型是否使用C#反射实现接口
- win11在dev渠道升级怎么保留原来的文件 windows11渠道升级保留原文件的方法步骤
- elementary os java,吐槽ELEMENTARY OS系统/ELEMENTARY OS系列文章汇总
- python中关于命名的例子_Python()-类命名空间和对象/实例命名空间
- peterson算法p0流程图_Dekker算法与Peterson算法
- ❤️开发项目必备技能《Git用法集合》建议收藏❤️