[DP解题] 买卖股票的最佳时间问题之三
原题链接:https://leetcode.com/problems/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.
算法设计:
package com.bean.algorithm.dp;
public class BestTimeBuyAndSellStockIII {
public static int maxProfit(int[] prices) {
if (prices == null || prices.length == 0)
return 0;
int n = prices.length;
int profit = 0;
// scan from left
// left[i] keeps the max profit from 0 to i
int[] left = new int[n];
int min = prices[0];
for (int i = 1; i < n; i++) {
left[i] = Math.max(left[i - 1], prices[i] - min);
min = Math.min(min, prices[i]);
}
// scan from right
// right[i] keeps the max profit from i to n - 1
int[] right = new int[n];
int max = prices[n - 1];
for (int i = n - 2; i >= 0; i--) {
right[i] = Math.max(right[i + 1], max - prices[i]);
max = Math.max(max, prices[i]);
profit = Math.max(profit, left[i] + right[i]);
}
return profit;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] demo = new int[] {3,3,5,0,0,3,1,4 };
//int[] demo = new int[] {1,2,3,4,5 };
//int[] demo = new int[] {3,3,5,0,0,3,1,4 };
int result = maxProfit(demo);
System.out.println("result = " + result);
}
}
运行结果:
result = 6
【拓展】
一道类似的题目:参考链接 https://blog.csdn.net/curson_/article/details/52021704
描述
最近越来越多的人都投身股市,阿福也有点心动了。谨记着“股市有风险,入市需谨慎”,阿福决定先来研究一下简化版的股票买卖问题。
假设阿福已经准确预测出了某只股票在未来 N 天的价格,他希望买卖两次,使得获得的利润最高。为了计算简单起见,利润的计算方式为卖出的价格减去买入的价格。
同一天可以进行多次买卖。但是在第一次买入之后,必须要先卖出,然后才可以第二次买入。
现在,阿福想知道他最多可以获得多少利润。
输入
输入的第一行是一个整数 T (T <= 50) ,表示一共有 T 组数据。
接下来的每组数据,第一行是一个整数 N (1 <= N <= 100, 000) ,表示一共有 N 天。第二行是 N 个被空格分开的整数,表示每天该股票的价格。该股票每天的价格的绝对值均不会超过 1,000,000 。
输出
对于每组数据,输出一行。该行包含一个整数,表示阿福能够获得的最大的利润。
样例输入
3
7
5 14 -2 4 9 3 17
6
6 8 7 4 1 -2
4
18 9 5 2
样例输出
28
2
0
提示
对于第一组样例,阿福可以第 1 次在第 1 天买入(价格为 5 ),然后在第 2 天卖出(价格为 14 )。第 2 次在第 3 天买入(价格为 -2 ),然后在第 7 天卖出(价格为 17 )。一共获得的利润是 (14 - 5) + (17 - (-2)) = 28
对于第二组样例,阿福可以第 1 次在第 1 天买入(价格为 6 ),然后在第 2 天卖出(价格为 8 )。第 2 次仍然在第 2 天买入,然后在第 2 天卖出。一共获得的利润是 8 - 6 = 2
对于第三组样例,由于价格一直在下跌,阿福可以随便选择一天买入之后迅速卖出。获得的最大利润为 0
解题思路:
用两个数组predp[ i ](第 i 天之前收益最大,包括第 i 天,且只买卖一次),post[ i ](第 i 天之后收益最大,包括第 i 天,且只买卖一次)。 给predp[ i ]赋值时,需要找到第 i 天(包括 i)之前最小的股市价格minn,然后用第 i 天的价格减去minn的值与predp[ i - 1 ]相比最大的就是predp[ i ]的值(因为predp[ i ]始终要保持第 i 天之前最大收益); 给postdp[ i ]赋值时,需要找到第 i 天(包括 i)之后最大的股市价格maxn,然后用maxn减去第 i 天的价格的值与postdp[ i + 1 ] 相比最大的就是postdp[ i ]的值(因为postdp[ i ]始终要保持第 i 天之后最大收益)。最后把predp[ i ] ,postdp[ i ] 相加,找到其中最大的值就是这几天交易两次最大的盈利。(第 i 天之前最大的收益 + 第 i 天之后最大的收益)。
代码后续补充...