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

解题思路一:

既然是两次交易的话,分为左右两个区间即可。先按照一次交易的思路算出交易的最大值,存一个数组里,然后从后往前遍历,找到符合条件的和最大的两个,JAVA实现如下:

	public int maxProfit(int[] prices) {
if (prices.length == 0)
return 0;
int[] oneProfit = new int[prices.length];
int buy_price = prices[0], profit = 0;
for (int i = 1; i < prices.length; i++) {
buy_price = Math.min(buy_price, prices[i]);
profit = Math.max(profit, prices[i] - buy_price);
oneProfit[i] = profit;
}
int res = oneProfit[prices.length - 1];
int sell_price = prices[prices.length - 1];
profit = 0;
for (int i = prices.length - 1; i >= 1; i--) {
sell_price = Math.max(sell_price, prices[i]);
profit = Math.max(profit, sell_price - prices[i]);
res = Math.max(res, profit + oneProfit[i - 1]);
}
return res;
}

解题思路二:

一种类似提前购买的思路:

参考https://leetcode.com/discuss/18330/is-it-best-solution-with-o-n-o-1%E3%80%82

JAVA实现如下:

    public int maxProfit(int[] prices) {
int hold1 = Integer.MIN_VALUE, hold2 = Integer.MIN_VALUE;
int release1 = 0, release2 = 0;
for(int i:prices){ // Assume we only have 0 money at first
release2 = Math.max(release2, hold2+i); // The maximum if we've just sold 2nd stock so far.
hold2 = Math.max(hold2, release1-i); // The maximum if we've just buy 2nd stock so far.
release1 = Math.max(release1, hold1+i); // The maximum if we've just sold 1nd stock so far.
hold1 = Math.max(hold1, -i); // The maximum if we've just buy 1st stock so far.
}
return release2; ///Since release1 is initiated as 0, so release2 will always higher than release1.
}

Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】的更多相关文章

  1. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  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. pt-online-schema-change原理解析(转)

    pt-online-schema-change原理解析 博客相关需要阅读 - zengkefu - 博客园 .pt-online-schema-change工具的使用限制: ).如果修改表有外键,除非 ...

  2. mac 下bash命令

    可以一行写一条命令 nginx uwsgi /Users/***/djangoprojects/bpmTest/uwsgi.ini 将上述命令保存成**.sh文件 这样,然后bash ***.sh 实 ...

  3. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. C#里判断字符串是否为纯数字

    c bool IsNumeric(string str) //接收一个string类型的参数,保存到str里 { if (str == null || str.Length == 0) //验证这个参 ...

  5. 时间迭代和BigDecimal操作

    常规小操作的代码: import java.math.BigDecimal; import java.sql.Timestamp; import java.text.SimpleDateFormat; ...

  6. linux中脚本扑捉(trap)信号问题

    扑捉ctrl+c信号: #!/bin/bash trap ; function trap() { echo "You press Ctrl+C."; echo "Exit ...

  7. 【AngularJS】Yeoman安装

    看不到PPT的请自行解决DNS污染问题.

  8. HDU 5301(Buildings-贪心构造)

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  9. 对于Json和对象转换的学习

    学习这个的用处有非常多的:        在传输数据过程中比較查看数据比較清晰,代码也较清晰.也能够避免split函数带来的隐藏bug 当然也有不足:        准备工具较繁琐,须要准备对象(当然 ...

  10. java使用Runtime.exec()运行windwos dos或linux shell命令

    使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试     实例代码: package com.bookoo.test.command; imp ...