【刷题-LeetCode】123 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 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 (i.e., you must sell the stock before you buy again).
Example 1:
Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
Solution
Approach1 以 i 为分界,左边是第一次交易能够获得的最大利润,右边是第二次,最后两边加起来取最大
Note:不能每次都计算一次,用数组存储能够获得的最大利润,否则会超时
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans = 0;
int n = prices.size();
if(n == 0)return ans;
int left[n] = {0}, right[n] = {0};
int min_price = prices[0];
for(int i = 1; i < n; ++i){
min_price = min(min_price, prices[i]);
left[i] = max(left[i-1], prices[i] - min_price);
}
int max_price = prices[n-1];
for(int i = n-2; i >= 0; --i){
max_price = max(max_price, prices[i]);
right[i] = max(right[i+1], max_price - prices[i]);
}
for(int i = 0; i < n; ++i){
ans = max(ans, left[i] + right[i]);
}
return ans;
}
};
Appraoch 2 每次取最值时针对全局的利润,设置4个变量:b1, s1, b2, s2
class Solution {
public:
int maxProfit(vector<int>& prices) {
int b1 = INT_MIN, b2 = INT_MIN;
int s1 = 0, s2 = 0;
for(int x : prices){
b1=max(b1,-x); //以低价买入
s1=max(s1,b1+x); //以高价卖出
b2=max(b2,s1-x); //低价买入,即结余要最大
s2=max(s2,b2+x); //高价卖出
}
return s2;
}
};
【刷题-LeetCode】123 Best Time to Buy and Sell Stock III的更多相关文章
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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]123. 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
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 ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
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
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)
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 ...
随机推荐
- CF415A Mashmokh and Lights 题解
Content 有 \(n\) 个灯,一开始都是亮着的. 有 \(m\) 次操作,每次操作按下开关 \(x\),按下之后所有编号 \(\geqslant x\) 的灯全部熄灭.问你所有的灯第一次被熄灭 ...
- SpringCloud使用GateWay网关前端请求请求跨域处理
增加配置类 CorsConfig.java import org.springframework.context.annotation.Bean; import org.springframework ...
- windows系统中远程登录ubuntu18服务器的桌面
下载:http://www.c-nergy.be/products.html 也可以直接服务器里 wget https://www.c-nergy.be/downloads/xRDP/xrdp-ins ...
- 金智维RPA培训(一)产品基础架构-RPA学习天地
1.产品组成分为:Server,control,agent三个组件,支持CS和BS架构.独有的中继服务器可以解决跨网段的问题,这里应该还是采用了多网卡模式. 其中:Agent负责对流程的执行工作.Co ...
- Tomcat高级特性及性能调优
Tomcat对Https的支持 HTTPS简介 Https,是以安全为目标的Http通道,在Http的基础上通过传输加密和身份认证保证了传输的安全性.HTTPS在HTTP的基础上加入SSL层,HTTP ...
- Android 崩溃错误
SIGSEGV ---段错误. 遇到此错误的可能情况是: 1.缓冲区溢出---通常由指针引用超出范围引起. 2.堆栈溢出---请记住默认堆栈大小为8192K. 3.我们的判断系统禁止文件访问---文件 ...
- java源码——对文件内容的查找和替换(开始写界面咯)
问题是:"键盘输入文件的路径.查找内容和替换内容,对指定路径的文件的内容进行查找和替换." 好久没写界面了,今天熟悉一下界面的书写和监听器操作. 这个问题的本身不是很难,重点应该是 ...
- leetcode1261在受污染的二叉树中查找元素
题目 一颗二叉树,树根值为0,父节点为x,则左子值为2x+1,右子为2x+2.现在只有树的结构,所有值都变为-1被污染了.求污染前是否存在某个值. 构建一次树,查询会调用多次. 题解 这道题还是比较简 ...
- ORA-14450: 试图访问已经在使用的事务处理临时表
需要对临时表动态添加列,经常碰到表在事务中被使用的情况,如果可以的话,可以现在只用临时表的时候先truncate,这样可以终止事务对当前临时表的占用. execute immediate('trunc ...
- Spring Boot实战一:搭建Spring Boot开发环境
一开始接触Spring Boot就感到它非常强大,也非常简单实用,遂想将其记录下来. 搭建Spring Boot工程非常简单,到:http://start.spring.io/ 下载Spring Bo ...