1. 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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 【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 ...

  5. 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 ...

  6. 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 ...

  7. Leetcode#123 Best Time to Buy and Sell Stock III

    原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. CF656C Without Text 题解

    Content 输入一个字符串 \(s\),遍历每一个字符,如果这个字符是小写字母,那么答案就加上这个字母的字母表序:否则,如果这个字符是小写字母,那么答案就减去这个字母的字母表序.求最后的答案. 字 ...

  2. [WPF] 实现 WPF 的 Inner Shadow

    在 WPF 中,我们通常用 DropShadow 做阴影效果,但都是做外阴影.内阴影(Inner Shadow)的话其实也不是不可以,就是有些曲折.这篇文章介绍几种做内引用的做法. 文章涉及到以下概念 ...

  3. JAVA获取当前请求的URL地址,包含请求链接中的参数

    /** * 获得当前访问的URL路径 * @param request * @return */ public static String getLocation(HttpServletRequest ...

  4. c++基础之operator =处理

    1.注意自我赋值 先看个例子: class A {}; A a ; a = a; // 注意这句 可能实际中,你不会这样做,但是实际中,是有可能的,并且这样做,也不违背语法. BUT, 如果上面的例子 ...

  5. 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...

  6. 关于wlw连接wordpress的问题

    前几天搭建好wordpress博客网站后,一直想和博客园一样,使用wlw发布文章.无奈遇到了难题,一直没有办法解决. 今天我看到一篇博客,遇到问题和我类似:尝试连接到您的日志时出错:基础连接已经关闭: ...

  7. JVM 内存布局

    JVM 内存布局规定了 Java 在运行过程中内存申请.分配.管理的策略,保证了 JVM 的高效稳定运行. 线程是否共享 Heap (堆区) 堆是 OOM 故障最主要的发生区域.它是内存区域中最大的一 ...

  8. API 网关功能

    反向代理和路由 - 大多数项目采用网关的解决方案的最主要的原因.给出了访问后端 API 的所有客户端的单一入口,并隐藏内部服务部署的细节. 负载均衡 - 网关可以将单个传入的请求路由到多个后端目的地. ...

  9. 【操作系统】Linux bash常用函数路径配置

    临时方法:export PATH=/usr/bin:/usr/sbin:/bin:/sbin长期方法:1.    vi /etc/profile2.    在最后插入并保存:    export PA ...

  10. LTD: Low Temperature Distillation for Robust Adversarial Training

    目录 概 主要内容 Chen E. and Lee C. LTD: Low temperature distillation for robust adversarial training. arXi ...