Best Time to Buy and Sell Stock IV
Last updated
Last updated
class Solution(object):
def maxProfit(self, k, prices):
"""
:type k: int
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
if k > len(prices) / 2: return self.easySolution(prices)
# 初始化 buy 和 sell
buy = []
sell = []
for i in range(k):
buy.append([0] * len(prices))
buy[i][0] = -prices[0]
for i in range(k + 1):
sell.append([0] * len(prices))
# 开始递推,依照上图即可
for j in range(1, k + 1):
for i in range(1, len(prices)):
buy[j - 1][i] = max(buy[j - 1][i - 1], sell[j - 1][i - 1] - prices[i])
sell[j][i] = max(sell[j][i - 1], buy[j - 1][i - 1] + prices[i])
return sell[-1][-1]
def easySolution(self, prices):
ret = 0
for i in range(1, len(prices)):
t = prices[i] - prices[i - 1]
if t > 0: ret += t
return ret public class Solution {
public int maxProfit(int k, int[] prices) {
if (prices == null || prices.length == 0) return 0;
int n = prices.length;
if (k > n / 2) return easySolution(prices);
int[] buy_last = null;
int[] buy_this = new int[n];
int[] sell = new int[n];
buy_this[0] = -prices[0];
for (int j = 0; j < k + 1; ++j) {
for (int i = 1; i < n; ++i) {
buy_this[i] = Math.max(buy_this[i - 1], sell[i - 1] - prices[i]);
sell[i] = Math.max(sell[i - 1], (j > 0 ? buy_last[i - 1] + prices[i] : 0));
}
buy_last = buy_this;
buy_this = new int[n];
buy_this[0] = -prices[0];
}
return sell[n - 1];
}
private int easySolution(int[] prices) {
int ret = 0;
for (int i = 1; i < prices.length; ++i) {
if (prices[i] > prices[i - 1]) {
ret += prices[i] - prices[i - 1];
}
}
return ret;
}
}class Solution {
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (n == 0) {
return 0;
}
if (k > n / 2) return easySolution(prices);
int[][] buy = new int[k + 1][n + 1];
int[][] sell = new int[k + 1][n + 1];
//注意此处初始化,第0天持股状态初始化为 -prices[0]
for (int i = 0; i <= k; ++i) {
buy[i][0] = -prices[0];
}
//这里注意,0次交易持股状态的最大value为之前所有price的最小值相反数
for (int i = 1; i <= n; ++i) {
buy[0][i] = Math.max(buy[0][i - 1], -prices[i - 1]);
}
for (int i = 1; i < k + 1; ++i) {
for (int j = 1; j < n + 1; ++j) {
buy[i][j] = Math.max(buy[i][j - 1], sell[i][j - 1] - prices[j - 1]);
sell[i][j] = Math.max(sell[i][j - 1], buy[i - 1][j - 1] + prices[j - 1]);
}
}
return sell[k][n];
}
private int easySolution(int[] prices) {
int ret = 0;
for (int i = 1; i < prices.length; ++i) {
if (prices[i] > prices[i - 1]) {
ret += prices[i] - prices[i - 1];
}
}
return ret;
}
}class Solution {
public int maxProfit(int k, int[] prices) {
if (prices == null || prices.length == 0) return 0;
int n = prices.length;
if (k > n / 2) return easySolution(prices);
int[][] buy = new int[k][prices.length + 1];
int[][] sell = new int[k + 1][prices.length + 1];
for (int i = 0; i < k; ++i) {
buy[i][0] = -prices[0];
}
for (int j = 1; j < prices.length + 1; ++j) {
for (int i = 1; i <= k; ++i) {
buy[i - 1][j] = Math.max(buy[i - 1][j - 1], sell[i - 1][j - 1] - prices[j - 1]);
sell[i][j] = Math.max(sell[i][j - 1], buy[i - 1][j - 1] + prices[j - 1]);
}
}
return sell[k][prices.length];
}
private int easySolution(int[] prices) {
int ret = 0;
for (int i = 1; i < prices.length; ++i) {
if (prices[i] > prices[i - 1]) {
ret += prices[i] - prices[i - 1];
}
}
return ret;
}
}