# 股票问题汇总

• 只能买卖一次
• 可以买卖无数次
• 可以买卖 k 次

## 解题思路

• 第几天
• 股票的价格
• 当前可获得的最大利润
• 手头有无股票
• 第几次交易

``````DP[i][j][0] -> 第 i 天，第 j 次交易，手头没有股票的最大利润
DP[i][j][1] -> 第 i 天，第 j 次交易，手头有股票的最大利润

``````Max(DP[n][0][0], DP[n][1][0], ..., DP[n][k][0])

``````DP[i][k][0] = Max(DP[i - 1][k][0], DP[i - 1][k - 1][1] + a[i])
DP[i][k][1] = Max(DP[i - 1][k][1], DP[i - 1][k - 1][0] - a[i])

## 参考代码

LeetCode 121. Best Time to Buy and Sell Stock

``````public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}

int[][][] dp = new int[prices.length][2][2];

dp[0][0][0] = 0; dp[0][0][1] = -prices[0];

for (int i = 1; i < prices.length; ++i) {
dp[i][1][0] = Math.max(dp[i - 1][1][0], dp[i - 1][0][1] + prices[i]);
dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][0][0] - prices[i]);
}

return dp[prices.length - 1][1][0];
}

LeetCode 122. Best Time to Buy and Sell Stock II

``````public int maxProfit(int[] prices) {
if ((prices == null) || (prices.length < 2)) {
return 0;
}

int[][] dp = new int[prices.length][2];

dp[0][0] = 0; dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; ++i) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}

return dp[prices.length - 1][0];
}

LeetCode 123. Best Time to Buy and Sell Stock III

``````public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}

// dp[i][j][k] -> day, time, whether have stock or not
int[][][] dp = new int[prices.length][3][2];

dp[0][0][1] = -prices[0]; dp[0][1][1] = -prices[0];
for (int i = 1; i < prices.length; ++i) {
dp[i][0][1] = Math.max(dp[i - 1][0][0] - prices[i], dp[i - 1][0][1]);

dp[i][1][0] = Math.max(dp[i - 1][0][1] + prices[i], dp[i - 1][1][0]);
dp[i][1][1] = Math.max(dp[i - 1][1][0] - prices[i], dp[i - 1][1][1]);

dp[i][2][0] = Math.max(dp[i - 1][1][1] + prices[i], dp[i - 1][2][0]);
}

return dp[prices.length - 1][2][0];
}

LeetCode 188. Best Time to Buy and Sell Stock IV

``````public int maxProfit(int k, int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}

if (k > prices.length / 2) {
int max = 0; int hold = prices[0];
for (int i = 1; i < prices.length; ++i) {
max += Math.max(0, prices[i] - prices[i - 1]);
}

return max;
}

// dp[i][j][0] -> at ith day, jth transaction, without stock in hand
// dp[i][j][1] -> at ith day, jth transaction, with stock in hand
int[][][] dp = new int[prices.length][k + 1][2];

// init
for (int i = 0; i <= k; ++i) {
dp[0][i][1] = -prices[0];
}

for (int i = 1; i < prices.length; ++i) {
dp[i][0][1] = Math.max(dp[i - 1][0][1], dp[i - 1][0][0] - prices[i]);
for (int j = 1; j <= k; ++j) {
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j - 1][1] + prices[i]);
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j][0] - prices[i]);
}
}

return dp[prices.length - 1][k][0];
}

LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

``````public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}

int[][] dp = new int[prices.length][3];

dp[0][0] = 0; dp[0][1] = -prices[0]; dp[0][2] = 0;

for (int i = 1; i < prices.length; ++i) {
dp[i][0] = Math.max(dp[i - 1][1] + prices[i], Math.max(dp[i - 1][0], dp[i - 1][2]));
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2] - prices[i]);
dp[i][2] = Math.max(dp[i - 1][0], dp[i - 1][2]);
}

return Math.max(dp[prices.length - 1][0], dp[prices.length - 1][2]);
}

LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

``````public int maxProfit(int[] prices, int fee) {
if (prices == null || prices.length < 2) {
return 0;
}

int[][] dp = new int[prices.length][2];

dp[0][0] = 0; dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; ++i) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}

return dp[prices.length - 1][0];
}

• 缪宇
4年前
• WeaponZhi
5年前
• 皮大大
2年前
• 斜月
1年前