Best Time to Buy and Sell Stock III [LeetCode]
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).
Solution: DP, caculate max profit both forward and backward, then return the max sum of them.
int maxProfit(vector<int> &prices) {
if(prices.size() <= )
return ;
vector<int> max_forward;
int lowest = prices[];
int max_profit = ;
max_forward.push_back();
for(int i = ; i < prices.size(); i ++ ){
int profit = prices[i] - lowest;
if(profit > max_profit)
max_profit = profit;
max_forward.push_back(max_profit);
lowest = min(prices[i], lowest);
}
int result = max_forward[max_forward.size() - ];
vector<int> max_backward;
int largest = prices[prices.size() - ];
max_profit = ;
for(int i = prices.size() - ; i >= ; i --) {
int profit = largest - prices[i];
max_profit = max(profit, max_profit);
result = max(result, max_profit + max_forward[i]);
largest = max(largest, prices[i]);
}
return result;
}
Best Time to Buy and Sell Stock III [LeetCode]的更多相关文章
- 123. Best Time to Buy and Sell Stock III ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Best Time to Buy and Sell Stock III leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- LeetCode 笔记23 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- Best Time to Buy and Sell Stock | & || & III
Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 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 ...
随机推荐
- json pickle time
多层装饰器: 字符串格式化: 当字符串格式化时 , %% 生成器 使用函数创造的 yield递归模块PY:模块其他:类库 先导入 后使用 自定义模块内置模块第三方模块为什么要有模块 将代码归类模块的内 ...
- gulp编译sass
前言:前段时间学习了sass语法,但是一直用的是"考拉"这个软件工具将我写的sass代码编译成css,然后再引用到项目里面去的,随着对sass的更加深入的了解,我开始尝试着将sas ...
- AngularJS过滤器
1.过滤器可以使用一个管道字符(|)添加到表达式和指令中. 再次强调一点,所有的运行都要加入angularJS的库文件(类似JQuery的引用) 常见的AngularJS 过滤器 (1) lowerc ...
- div嵌套导致子区域margin-top失效不起作用的解决方法
有两个嵌套关系的div,如果外层div的父元素padding值为0, 那么内层div的margin-top或者margin-bottom的值会"转移"给外层div,使父元素产生上外 ...
- JavaScript局部变量和全局变量的理解
原文链接:http://www.cnblogs.com/eric-qin/p/4166552.html JavaScript局部变量和全局变量的理解 1 2 3 4 5 6 7 8 9 10 &l ...
- (转载)The One Sign You Will Be Rich-(by Brian de Haaff Founder and CEO Aha! -- world's #1 product roadmap software)
When I was studying Philosophy at Berkeley, a friend told me that she could tell who was going to be ...
- Android开发——搭建最新版本的Android开发环境
http://www.cnblogs.com/smyhvae/p/3788534.html http://www.cnblogs.com/xdp-gacl/p/4322165.html http:// ...
- Android runtime Exception 整理
一般面试中java Exception(runtimeException )是必会被问到的问题 常见的异常列出四五种,是基本要求.更多的....需要注意积累了 常见的几种如下: NullPoi ...
- php变量赋值给js
1 2 3 4 5 6 7 8 $(document).ready(function(){ <?php $f="'name'"?> ...
- JS对象深刻理解 - 1
JavaScript创建对象 JavaScript 有Date.Array.String等这样的内置对象,功能强大使用简单,人见人爱,但在处理一些复杂的逻辑的时候,内置对象就很无力了,往往需要开发 ...