[LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Posted 轻风舞动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV相关的知识,希望对你有一定的参考价值。
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 k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example 1:
Input: [2,4,1], k = 2 Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:
Input: [3,2,6,5,0,3], k = 2 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.
123. Best Time to Buy and Sell Stock III 这题是最多能交易2次,而这题是最多k次。
要用动态规划Dynamic programming来解,需要两个递推公式来分别更新两个变量local和global。定义local[i][j]为在到达第i天时最多可进行j次交易并且最后一次交易在最后一天卖出的最大利润,此为局部最优。然后我们定义global[i][j]为在到达第i天时最多可进行j次交易的最大利润,此为全局最优。它们的递推式为:
local[i][j] = max(global[i - 1][j - 1] + max(diff, 0), local[i - 1][j] + diff)
global[i][j] = max(local[i][j], global[i - 1][j])
Java:
public int maxProfit(int k, int[] prices) { int len = prices.length; if (k >= len / 2) return quickSolve(prices); int[][] t = new int[k + 1][len]; for (int i = 1; i <= k; i++) { int tmpMax = -prices[0]; for (int j = 1; j < len; j++) { t[i][j] = Math.max(t[i][j - 1], prices[j] + tmpMax); tmpMax = Math.max(tmpMax, t[i - 1][j - 1] - prices[j]); } } return t[k][len - 1]; } private int quickSolve(int[] prices) { int len = prices.length, profit = 0; for (int i = 1; i < len; i++) // as long as there is a price gap, we gain a profit. if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1]; return profit; }
Python:
class Solution(object): # @return an integer as the maximum profit def maxProfit(self, k, prices): if k >= len(prices) / 2: return self.maxAtMostNPairsProfit(prices) return self.maxAtMostKPairsProfit(prices, k) def maxAtMostNPairsProfit(self, prices): profit = 0 for i in xrange(len(prices) - 1): profit += max(0, prices[i + 1] - prices[i]) return profit def maxAtMostKPairsProfit(self, prices, k): max_buy = [float("-inf") for _ in xrange(k + 1)] max_sell = [0 for _ in xrange(k + 1)] for i in xrange(len(prices)): for j in xrange(1, min(k, i/2+1) + 1): max_buy[j] = max(max_buy[j], max_sell[j-1] - prices[i]) max_sell[j] = max(max_sell[j], max_buy[j] + prices[i]) return max_sell[k]
C++:
class Solution { public: int maxProfit(int k, vector<int> &prices) { if (prices.empty()) return 0; if (k >= prices.size()) return solveMaxProfit(prices); int g[k + 1] = {0}; int l[k + 1] = {0}; for (int i = 0; i < prices.size() - 1; ++i) { int diff = prices[i + 1] - prices[i]; for (int j = k; j >= 1; --j) { l[j] = max(g[j - 1] + max(diff, 0), l[j] + diff); g[j] = max(g[j], l[j]); } } return g[k]; } int solveMaxProfit(vector<int> &prices) { int res = 0; for (int i = 1; i < prices.size(); ++i) { if (prices[i] - prices[i - 1] > 0) { res += prices[i] - prices[i - 1]; } } return res; } };
类似题目:
[LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
[LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
[LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
All LeetCode Questions List 题目汇总
以上是关于[LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV的主要内容,如果未能解决你的问题,请参考以下文章
leetcode 188. Best Time to Buy and Sell Stock IV
[LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV
LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)