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 ...
随机推荐
- Dynamics AX 2012 R3 仓库和运输管理系列 - 仓库管理模块安装与配置
在AX 2012 R3版本中,新增了仓库和运输管理模块,同时提供了一个在移动设备上进行仓库管理工作的网站.在这个系列里,与Reinhard一起,了解仓库和运输管理模块吧. 需要注意的是 ...
- Dynamics AX 2012 R2 SSRS报表在VS2010中预览没有数据
今天,Reinhard 在VS中制作SSRS报表,预览的时候发现显示不出数据. 仔细检查了数据处理环节和临时表里的数据,都发现没有问题. 用同事的账号登陆同样的开发环境,发现他的账号可以在VS中预览到 ...
- [已解决][HTML5]基于WebSocket开发小蝌蚪应用
前端时间在网上看到别人用WebSocket开发的小蝌蚪应用很炫酷,不过是php,于是想着用java也实现一套, 找到前端 https://github.com/danielmahal/Rumpetro ...
- web项目 log4j2的路径问题
项目中用到log4j2记录日志,结果运行的时候总也不见log文件的产生. 查看官方文档得知,在web项目中使用log4j2需要加入log4j-web包 log4j2.xml <?xml vers ...
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
/// <summary> /// 返回指定目录下的所有文件信息 /// </summary> /// <param name="strDirectory&qu ...
- 使用JavaScript访问子节点方法elementNode.childNodes时,需要注意的地方
有这样一个HTML结构 <div> javascript <p>javascript</p> <div>jQuery</div> <h ...
- Boost学习笔记(一) 什么是Boost
Boost库是一个功能强大.构造精巧,跨平台.开源并且完全免费的C++程序库
- UVALive 3177 长城守卫
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- Using GET_APPLICATION_PROPERTY in Oracle D2k Forms
Using GET_APPLICATION_PROPERTY in Oracle D2k Forms DescriptionReturns information about the current ...
- elasticsearch 初学 笔记(1)
使用 curl进行数据的的格式, 1.下载curl,配置路径 格式 C:\Users\Administrator>curl -XPUT http://localhost:9200/dept/em ...