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 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】的更多相关文章
- 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 ...
- 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 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 ...
- 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
原题地址 最直观的想法就是划分成两个子问题,每个子问题变成了:求在某个范围内交易一次的最大利润 在只能交易一次的情况下,如何求一段时间内的最大利润?其实就是找股价最低的一天买进,然后在股价最高的一天卖 ...
- 【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 ...
- 【刷题-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 ...
随机推荐
- 好工具MyEclise2016 CI下载
地址:http://pan.baidu.com/s/1gfBw9Ab 安装后,点开crack目录,按步骤走. 下面是我安装成功的画面.
- eclipse中文件文件夹高速定位,打开文件所在文件夹,在资源管理器中查看
viewFile.bat (打开选中的文件获取目录) Explorer/e,/select,%1 viewjava.bat (打开选中的文件名称相应的.java文件) @echo off set ca ...
- zabbix agent shell一键安装脚本
#!/bin/bash basepath=$(cd ``; pwd) SHELL_DIR="${basepath}/shell" PACKAGE_DIR="${basep ...
- 我如何添加一个空目录到Git仓库?
新建了一个仓库,只是创建一些目录结构,还不里面放什么,要放的内容还没有,还不存在,应该怎么办呢? Git 是不跟踪空目录的,所以需要跟踪那么就需要添加文件! 也就是说 Git 中不存在真正意义上的空目 ...
- java使用Runtime.exec()运行windwos dos或linux shell命令
使用Runtime.exec()运行windwos dos或linux shell命令,按实际情况具体测试 实例代码: package com.bookoo.test.command; imp ...
- SpringBoot学习之常用注解
@SpringBootAppliaction:通常注解写在SpringBoot启动类中,主要包括三个作用: 1.@Configuration表示将该类作用springboot配置文件类. 2.@Ena ...
- HDOJ2084数塔问题
数塔问题 题目要求从顶层走究竟层.若每一步仅仅能走到相邻的结点,求经过的结点的数字之和最大值. 非常经典的DP,能够这样考虑,要求从塔顶到塔底最大路径之和.计算时能够考虑自底向上,走最后一步所选的数一 ...
- ubuntu16.04 Cmake学习二
本节主要总结编译程序的时候使用了第三方库的情况,以调用开源opencv-2.4.9为例子,具体安装详见http://www.cnblogs.com/xsfmg/p/5900420.html. 工程文件 ...
- mysql中把空值放在最后,有值的数据放在前面
order by column is null,column; 如果:order by column,则column中空值的数据放在最前面,有数据的放在后面
- 在一个JS文件中引用另一个JS文件
方法一,在调用文件的顶部加入下例代码: document.write(”<script language=javascript src=’/js/import.js’></scrip ...