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 ...
随机推荐
- Android 子线程测试
private volatile boolean mStopped = false; private int i; TextView tv1; TextView tv2; @Override prot ...
- C#:获取环境信息
外部环境数据1.需要管理员权限2.需要安装office2003以上完整版3.需要安装flash reader 10.0以上4.需要安装adodb reader;Adobe Acrobat X Pro; ...
- Lucky and Good Months by Gregorian Calendar - POJ3393模拟
Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Description Have ...
- Git使用指南(1)——Git配置命令
配置用户信息 git config --global user.name bongxin git config --global user.email bongxin@yeah.net 配置文本编辑器 ...
- 一首诗,致亲爱的csdn
来自csdn的Rachel-Zhang姐姐 还记得--致亲爱的csdn 还记得你年轻时的摸样? 简单的文字,无瑕的脸庞. 现在的你,满脸风霜. 五粮液的广告,在我的文章中久久荡漾. 还记得当初的梦想? ...
- git无法定位程序输入点libiconv
使用git clone时,报以下错误: 解决方案: 将git\bin\下的libiconv-2.dll复制到\git\libexec\git-core\下即可
- 描述性统计分析-用脚本将统计量函数批量化&分步骤逐一写出
计算各种描述性统计量函数脚本(myDescriptStat.R)如下: myDescriptStat <- function(x){ n <- length(x) #样本数据个数 m &l ...
- 解迷宫的C++的未完善编程代码........请大神们帮忙改善下.........
这...................................................................... 我也是醉了 看不太懂,大神们求解............ ...
- linux下与windows下的换行符
[原文有些许错误,已作了修改] 回车符号和换行符号产生背景 关于“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别.在计算机还没有出现之前,有一种叫做电传 ...
- 为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉?
为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉? 就像这样 两个div高度相同,第二个我写了一个1当作 有内容吧,它就下沉了... 奇怪... ...