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 ...
随机推荐
- 深入js的面向对象学习篇——温故知新(一)
在学习设计模式前必须要知道和掌握的***. 为类添加新方法: Function.prototype.method = function(name,fn) { this.prototype[name] ...
- kafka.network.SocketServer分析
当Kafka启动时,会启动这个SocketServer来接收客户端的连接,处理客户端请求,发送响应. 这个类的注释说明了这个socket server的结构 /** * An NIO socket s ...
- Python/Ruby/Go/Node 之四国大战
Python Flask vs Ruby Sinatra vs Go Martini vs Node Express 本文授权转载自 zybuluo 博客. 题外话一: 最近一段时间,Cloud In ...
- C语言:将16进制字符串转化为int类型值
将16进制字符串值转换为 int 整型值 此例中用 "1de" 作为测试字符串,实现代码如下: #include <stdio.h> #include <stdl ...
- Eclipse不能自动编译 java文件的解决方案
前段时间出现了eclipse 不自动编译java文件的问题,在网上找了好长时间,总算把问题解决了,现在把这个问题的解决方法总结一下. 1,看看project -- Build Automaticall ...
- 李洪强iOS开发之拓展篇—UIDynamic(简单介绍)
iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能 ...
- the service mysql56 was not found in the Windows services的解决办法
mysql无法启动,无法改变状态-CSDN论坛-CSDN.NET-中国最大的IT技术社区 http://bbs.csdn.net/topics/390943788 具体描述: 关闭,重启mysql ...
- editplus的配置文件来支持sql语法高亮【转】
editplus默认是没有sql语法高亮的,原因是它的内部没有sql.stx的这样一个语法文件 我们自己在 EditPlus 的安装目录下面新建一个文件名为sql.stx,然后打开editplus ...
- semantic versioning语义化版本号
语义化版本号 是由github创始人 Tom Preston-Werner 发起的一个关于软件版本号的命名规范,关于这个规范详细的说明可以在 官网 查看,也可访问其 GitHub项目页面 ,官网文档: ...
- WinAPI——Windows 消息
消息 值 注释 WM_NULL $0000 WM_CREATE $0001 WM_DESTROY $0002 WM_MOVE $0003 WM_SIZE $0005 WM_AC ...