LeetCode刷题 Day50
123. Best Time to Buy and Sell Stock III
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: prices = [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: prices = [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: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
思路: 难点在于如何确定两次交易,两次交易就意味着5中状态。 dp[i][0] 没有操作 dp[i][1] 第一次买入 dp[i][2] 第一次卖出 dp[i][3] 第二次买入 dp[i][4] 第二次卖出
步骤:
- dp index和value: i代表第几天,0,1,2,3,4分表代表 没有操作,第一次买入,第一次卖出,第二次买入,第二次卖出
- 递推公式: dp[i][0] = dp[i - 1][0]; dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]); dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]); dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
- 初始化: dp[0][1] = -prices[0], dp[0][3] = -prices[0];
- 遍历顺序: 顺序
代码:
var maxProfit = function(prices) {
let length = prices.length;
const dp = new Array(length).fill(0).map(item => new Array(5).fill(0));
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (let i = 1; i < length; i++) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return dp[length - 1][4];
};
时间复杂度: O(n) 空间复杂度: O(n * 5);
188. Best Time to Buy and Sell Stock IV
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.
Find the maximum profit you can achieve. You may complete at most k transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:
Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
思路: 和股票III思路类似,只是确定次数变成了k次
步骤:
- dp index和value: i代表第几天,0,1,2,3,4分表代表 没有操作,第一次买入,第一次卖出,第二次买入,第二次卖出
- 递推公式: dp[i][j + 1] = Math.max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); dp[i][j + 2] = Math.max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
- 初始化, 奇数项为-prices[0]:
for (let i = 1; i < 2 * k; i += 2) { dp[0][i] = -prices[0]; } - 遍历顺序: 顺序
代码:
var maxProfit = function(k, prices) {
let length = prices.length;
if (prices === null || length < 2 || k == 0) {
return 0;
}
let dp = Array(length).fill(0).map(item => Array(2 * k + 1).fill(0));
for (let i = 1; i < 2 * k; i += 2) {
dp[0][i] = -prices[0];
}
for (let i = 1; i < length; i++) {
for (let j = 0; j < 2 * k; j += 2) {
dp[i][j + 1] = Math.max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
dp[i][j + 2] = Math.max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
}
}
return dp[length - 1][2 * k];
};
时间复杂度: O(2kn) 空间复杂度: O(2kn)