leetcode—Best Time to Buy and Sell stocks III
1.题目描述
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).
2.解法分析
限定了交易次数之后题目就需要我们略微思考一下了,由于有两次交易的机会,那么我们选定一个时间点ti,将此时间点作为两次交易的支点的话,必然有:
t0….ti之内满足最佳交易原则,ti-tn天也满足最佳交易原则,那么这就是一个动态规划的策略了,于是有下面的代码:
class Solution {public:int maxProfit(vector<int> &prices) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(prices.size() <=1)return 0;vector<int>::iterator iter;for(iter=prices.begin();iter!=prices.end()-1;++iter){*iter = *(iter+1) - *iter;}prices.pop_back();vector<int>accum_forward;vector<int>accum_backward;int max = 0;int subMax = 0;for(iter=prices.begin();iter!=prices.end();++iter){subMax += *iter;if(subMax > max)max=subMax;elseif(subMax<0)subMax = 0;accum_forward.push_back(max);}vector<int>::reverse_iterator riter;max =0 ;subMax = 0;for(riter=prices.rbegin();riter!=prices.rend();++riter){subMax +=*riter;if(subMax >max)max = subMax;elseif(subMax<0)subMax=0;accum_backward.push_back(max);}max =0;int len = accum_forward.size();for(int i=0;i<len-1;++i){if((accum_forward[i]+accum_backward[len-i-2])>max)max = accum_forward[i]+accum_backward[len-i-2];}return max>accum_forward[len-1]?max:accum_forward[len-1];}};
ps:做完题之后提交,发现老是AC不了,有个case总是解决不了,本来以为是自己代码写得有问题,检查了半天没发现错误,于是开始看别人怎么写,结果发现别人AC的代码也过不了,猜想可能系统还是做得不完善,应该是后台的线程相互干扰了,过了一段时间果然同样的代码又可以AC了。在这段过程中,看别人写的代码,发现了一个比我简洁一些的写法,虽然我么你的复杂度是一样的,但是此君代码量比我的小一点,以后学习学习,另外,一直不知道vector还可以预先分配大小,从这个代码里面也看到了,算是有所收获。附代码如下:
class Solution {public:int maxProfit(vector<int> &prices) {// null checkint len = prices.size();if (len==0) return 0;vector<int> historyProfit;vector<int> futureProfit;historyProfit.assign(len,0);futureProfit.assign(len,0);int valley = prices[0];int peak = prices[len-1];int maxProfit = 0;// forward, calculate max profit until this timefor (int i = 0; i<len; ++i){valley = min(valley,prices[i]);if(i>0){historyProfit[i]=max(historyProfit[i-1],prices[i]-valley);}}// backward, calculate max profit from now, and the sum with historyfor (int i = len-1; i>=0; --i){peak = max(peak, prices[i]);if (i<len-1){futureProfit[i]=max(futureProfit[i+1],peak-prices[i]);}maxProfit = max(maxProfit,historyProfit[i]+futureProfit[i]);}return maxProfit;}};
leetcode—Best Time to Buy and Sell stocks 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
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- 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 ...
随机推荐
- 深入理解SQL注入绕过WAF与过滤机制
知己知彼,百战不殆 --孙子兵法 [目录] 0x0 前言 0x1 WAF的常见特征 0x2 绕过WAF的方法 0x3 SQLi Filter的实现及Evasion 0x4 延伸及测试向量示例 0x5 ...
- uva 10791
还算比较水的一个数学题 求因子的最小和 总是用小的数去除 注意特判 是用int不行哦........ #include <cstdio> #include <cmath> ...
- 重新学struct,边界对齐,声明……与Union的区别
在内存中,编译器按照成员列表顺序分别为每个结构体变量成员分配内存,当存储过程中需要满足边界对齐的要求时,编译器会在成员之间留下额外的内存空间. 如果想确认结构体占多少存储空间,则使用关键字sizeof ...
- use sql trigger call java function
Use UDF sys_exec to do this. You can use this link to use sys_exec function. It says, sys_exec sys_e ...
- nginx静态资源分离部署
修改nginx.conf文件,用于nginx处理静态资源. 主要配置如下(在server配置中加入location配置即可): server { listen 80; server_name 123. ...
- 传智博客(JavaWeb方面的所有知识)听课记录(经典)
一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本 ...
- RedMine项目管理系统邮件推送设置(Windows环境)
RedMine项目管理系统有邮箱推送功能,当Bug,安全漏洞等内容被修改.解决.评论的时候,系统会通过邮件 及时的通知你的团队和客户.邮件通知的环节.形式.时间.接受人均可定制,功能十分实用. 下面是 ...
- Python之异常篇 [待更新]
简介 当你的程序中出现某些 异常的 状况的时候,异常就发生了.例如,当你想要读某个文件的时候,而那个文件不存在.或者在程序运行的时候,你不小心把它删除了.上述这些情况可以使用异常来处理. 假如你的程序 ...
- Winform 数据验证
http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx 总结:1. CancelEventArgs e ,调用e ...
- 智传播客hadoop视频学习笔记(共2天)
第一天:1.答疑解惑• 就业前景• 学习hadoop要有什么基础• hadoop会像塞班一样,热一阵子吗• hadoop学习起来容易还是困难• 课堂上的学习方法(所有实验必须按照要求做,重原 ...