给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
输入: [3,3,5,0,0,3,1,4]
输出: 6
解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
- 看到这题最开始的想法:
如果是单次购买则找一个连续增张的区间 区间起始买入 结束卖掉
因为限制最多买两次 所以找到所有连续增长的区间 去起止差值最大的两个
class Solution {
public int maxProfit(int[] prices) {
int[] inters = new int[prices.length];
int interIndex = 0;
int lastStart = 0;
int cursor = lastStart + 1;
// 寻找连续增长的区间 记录区间起始的差值
while (cursor < prices.length) {
if (prices[cursor] < prices[cursor - 1]) {
if (cursor - 1 != lastStart) {
inters[interIndex++] = prices[cursor - 1] - prices[lastStart];
}
lastStart = cursor;
}
cursor++;
}
if (prices[cursor - 1] > prices[lastStart] && lastStart != (cursor - 1)) {
inters[interIndex++] = prices[cursor - 1] - prices[lastStart];
}
if (interIndex == 0) {
return 0;
}
if (interIndex == 1) {
return inters[0];
}
// 移动最大的两位数到前面
for (int j = 0; j < 2; j++) {
for (int i = j + 1; i < interIndex; i++) {
if (inters[i] > inters[j]) {
swap(inters, i, j);
}
}
}
return inters[0] + inters[1];
}
private void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
这样做的问题在于 连续增长的区间确实可以达到最大值,但是可能会增长买卖的次数 如下 如果是1~4 2-5 1-9这样利润最大 但是需要买卖三次
但如果不去连续增长的区间 1-5 1-9这样计算出来的值是比上述三个区间两个最大的加和更优
1 2 3 4 2 5 1 9
- 寻找连续增长的区间不可行 能想到的只有遍历所有的情况了 用递归描述一下算法
public class StockV2 {
public int maxProfit(int[] prices) {
return dp(prices, 0, false, 2);
}
// occupy 是否已经持有一张股票 leftCount 剩余可交易次数 (每当卖出一次 视为完成交易一次)
// 从第i天开始交易股票 以当前的状态 可获取最大利润
private int dp(int[] prices, int i, boolean occupy, int leftCount) {
if (leftCount <= 0 || i >= prices.length) {
return 0;
}
int currPrice = prices[i];
int max;
if (!occupy) {
// 如果当前不持有股票 可以选择买或不买
max = Math.max(dp(prices, i+1, true, leftCount) - currPrice,
dp(prices, i+1, false, leftCount));
} else {
// 如果当前持有股票可以选择 卖或不卖
max = Math.max(dp(prices, i+1, false, leftCount - 1) + currPrice,
dp(prices, i+1, true, leftCount));
}
return max;
}
public static void main(String[] args) {
int[] array = new int[] {7,6,4,3,1};
System.out.println(new StockV2().maxProfit(array));
}
}
这个版本提交运行超时
- 依照记忆法优化了一下上面的算法 通过map保存中间结果 通过了更多的样例 但还是超时
class Solution {
// map 保存中间结果 避免重复计算
private Map<String, Integer> recordMap = new HashMap<>();
public int maxProfit(int[] prices) {
return dp(prices, 0, false, 2);
}
// occupy 是否已经持有一张股票 leftCount 剩余可交易次数 (每当卖出一次 视为完成交易一次)
// 从第i点开始 可获取最大利润
private int dp(int[] prices, int i, boolean occupy, int leftCount) {
if (leftCount <= 0 || i >= prices.length) {
return 0;
}
String cacheKey = genKey(i, occupy, leftCount);
if (recordMap.get(cacheKey) != null) {
return recordMap.get(cacheKey);
}
int currPrice = prices[i];
int max;
if (!occupy) {
max = Math.max(dp(prices, i+1, true, leftCount) - currPrice,
dp(prices, i+1, false, leftCount));
} else {
max = Math.max(dp(prices, i+1, false, leftCount - 1) + currPrice,
dp(prices, i+1, true, leftCount));
}
recordMap.put(cacheKey, max);
return max;
}
private String genKey(int i, boolean occupy, int leftCount) {
return String.valueOf(i) + occupy + leftCount;
}
}
- 修改为数组操作模拟递归流程 通过
class Solution {
public int maxProfit(int[] prices) {
int [] [] [] dp = new int[prices.length + 1][2] [3];
for (int i = 0; i < prices.length +1; i++){
for(int j = 0; j<2; j++) {
for(int k = 0; k <3; k++) {
dp[i][j][k] =0;
}
}
}
for (int i = prices.length - 1; i >= 0; i--) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k <= 2; k++) {
if (k == 0) {
continue;
}
int max;
if (j == 0) {
max = Math.max(dp[i + 1][0][k], dp[i+1][1][k] - prices[i]);
} else {
max = Math.max(dp[i+1][1][k], dp[i+1][0][k-1] + prices[i]);
}
dp[i][j][k] = max;
}
}
}
return dp[0][0][2];
}
}