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:

题目

和之前一样,这次你至多能买卖两次。

思路

1. Split the array into two parts, one trade each.

2. Say that f(i) stands for max profit in [0,i] , g(i) stands for max profix in [i, n-1] , then update and finally return max(f(i) + g(i))

代码

 // Best Time to Buy and Sell Stock III
// 时间复杂度O(n),空间复杂度O(n)
public class Solution {
public int maxProfit(int[] prices) {
if (prices.length < 2) return 0; final int n = prices.length;
int[] f = new int[n];
int[] g = new int[n]; for (int i = 1, valley = prices[0]; i < n; ++i) {
valley = Math.min(valley, prices[i]);
f[i] = Math.max(f[i - 1], prices[i] - valley);
} for (int i = n - 2, peak = prices[n - 1]; i >= 0; --i) {
peak = Math.max(peak, prices[i]);
g[i] = Math.max(g[i], peak - prices[i]);
} int max_profit = 0;
for (int i = 0; i < n; ++i)
max_profit = Math.max(max_profit, f[i] + g[i]); return max_profit;
}
}

[leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三的更多相关文章

  1. [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

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

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

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

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

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

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

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

随机推荐

  1. 页面中onclick事件引号问题

    第一种:html中onclick调用事件 <p id="txt" onclick="changeSize()">加括弧的changeSize()&l ...

  2. 关于maven中的快照版本(snapshot)与正式版本(release)解析。

    Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...

  3. Error building Player: Exception: Could not start java

    4.6.1发布Flash版本出错.解决方法:把C:\Windows\System32\java.exe复制到C:\Windows\SysWOW64下即可

  4. 转: HTML5之placeholder属性以及如何更改placeholder属性中文字颜色

    今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中途遇到些问题,查找资料后特来总结一下. 熟悉HTML5的人应 ...

  5. 【原创】逆向练习(CrackMe)

    Write Up 登录的两个方法 方法1.用IDA分析 新版测试题.exe.在Strings Window中查找有一定意义的串. 从上面的窗口中,发现了CyberSwat和passwordisme这两 ...

  6. Android通过adb命令Debug调试

    Android Debug后IDE执行的命令: / ::: Launching module_app $ adb push C:\fastwork\Projects\project\CJPT\modu ...

  7. delphi控制本计算机和远程计算机关机等

    unit mainunit; {远程关机源码} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Con ...

  8. 吴裕雄 12-MySQL WHERE 子句

    以下是 SQL SELECT 语句使用 WHERE 子句从数据表中读取数据的通用语法:SELECT field1, field2,...fieldN FROM table_name1, table_n ...

  9. CFDA

    cfda数据抓取 1.网站数据是加密的,需要浏览器进行数据解析 2.网址url有js加密 3.PhantomJS无法解析数据, chrome无法获取数据,所有最终选择用Firefox浏览器 impor ...

  10. CSS 选用字体

    font-family CSS的font-family属性可以指定(选择)字体. 语法: 1 font-family: Gill Sans Extrabold, sans-serif; font-fa ...