【刷题-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 ...
随机推荐
- CF761A Dasha and Stairs 题解
Content 给定两个数 \(n,m\),试问是否有区间里面有 \(n\) 个奇数和 \(m\) 个偶数. 数据范围:\(0\leqslant n,m\leqslant 100\). Solutio ...
- win10修改Docker Desktop Installer的默认安装目录
Docker Desktop Installer软件默认安装会装在C:\Program Files\Docker的目录下,默认是不能修改的,但是我们可以通过设置软链接的方式把安装默认弄到其他盘 我们先 ...
- JS监控浏览器窗口是否活动,最小化操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【LeetCode】796. Rotate String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- C. Hongcow Builds A Nation
C. Hongcow Builds A Nation time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 深度学习中常见的 Normlization 及权重初始化相关知识(原理及公式推导)
Batch Normlization(BN) 为什么要进行 BN 防止深度神经网络,每一层得参数更新会导致上层的输入数据发生变化,通过层层叠加,高层的输入分布变化会十分剧烈,这就使得高层需要不断去重新 ...
- JSP、JSTL标签、EL表达式
JSP.JSTL标签.EL表达式 1.EL表达式:${} 功能: 获取数据 执行运算 获取web开发的常用对象 2.JSP标签 例如: jsp标签还有很多功能,这里只列举出一种. <jsp:fo ...
- 编写Java程序,使用循环结构打印出九九乘法表
编写Java程序,使用循环结构打印出九九乘法表 效果如下: 实现代码: public class Multiplication99 { public static void main(String[] ...
- 加深理解Java异常概念并熟记5个最常见的运行时异常
加深理解Java异常概念并熟记5个最常见的运行时异常 说明Error与Exception的联系和区别有哪些? 列举最常见的5个运用时异常. 1.Error和Exception的联系和区别: Error ...