DP: 股票买卖
Leetcode 121. Best Time to Buy and Sell Stocks
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() < 2) return 0;
vector<vector<int>> dp(prices.size(), vector<int>(2, 0));
// initialize
dp[0][0] = -prices[0]; // hold or buy stock
dp[0][1] = 0; // no stock on hand or sell
for (int i = 1; i < prices.size(); i ++)
{
// rest or buy
dp[i][0] = max(dp[i - 1][0], -prices[i]);
// rest or sell
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[dp.size() - 1][1];
}
};
Leetcode 122. Best Time to Buy and Sell Stocks II
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() < 2) return 0;
vector<vector<int>> dp(prices.size(), vector<int>(2, 0));
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (int i = 1; i < prices.size(); i ++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);// hold stock or buy stock??
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[dp.size() - 1][1];
}
};
Leetcode 123. Best Time to Buy and Sell Stocks III
状态有三个:第一个是天数,第二个是允许交易的最大次数,第三个是当前的持有状态
dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k - 1][1] - prices[i])
= max(rest, buy)
- 我昨天持有股票,且截止到昨天的最大交易次数限制为k,然后今天选择rest,所以还持有昨天就持有的股票,最大交易次数限制为k
- 我昨天没有持有股票,且昨天最大交易次数限制为 k - 1, 今天我选择buy,所以今天持有股票,最大交易次数为k
dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k][0] + prices[i])
= max(rest, sell)
- 我昨天没有持有股票,且截止到昨天的最大交易次数限制为k,然后今天选择rest,所以今天还是没有持有股票,最大交易次数限制为k
- 我昨天持有股票,且昨天最大交易次数限制为 k, 今天我选择sell,所以今天没有持有股票,最大交易次数仍为k
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() < 2) return 0;
vector<vector<vector<int>>> dp(prices.size(), vector<vector<int>>(3, vector<int>(2, 0)));
// dp[i][j][k] = ith day, jth transactions, k = 0 => rest or buy, k = 1 => rest or sell
for (int i = 0; i < prices.size(); i ++)
{
for (int j = 2; j >= 1; j --)
{
if (i - 1 == -1)
{
dp[i][j][0] = -prices[i];
continue;
}
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j - 1][1] - prices[i]);
dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j][0] + prices[i]);
}
}
return dp[prices.size() - 1][2][1];
}
};
Leetcode 188. Best Time to Buy and Sell Stock IV
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if (prices.size() < 2) return 0;
if (prices.size() / 2 < k)
{
vector<vector<int>> dp(prices.size(), vector<int>(2, 0));
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (int i = 1; i < prices.size(); i ++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);// hold stock or buy stock??
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[dp.size() - 1][1];
}
vector<vector<vector<int>>> dp(prices.size(), vector<vector<int>>(k + 1, vector<int>(2, 0)));
for (int i = 0; i < prices.size(); i ++)
{
cout << i << endl;
for (int j = 1; j <= k; j ++)
{
if (i - 1 == -1)
{
dp[i][j][0] = -prices[i];
continue;
}
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j - 1][1] - prices[i]);
dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 1][j][0] + prices[i]);
}
}
return dp[dp.size() - 1][k][1];
}
};
Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() < 2) return 0;
vector<vector<int>> dp(prices.size(), vector<int>(2, 0));
dp[0][0] = -prices[0];
dp[1][0] = max(dp[0][0], -prices[1]);
dp[1][1] = max(dp[0][1], dp[0][0] + prices[1]);
for (int i = 2; i < prices.size(); i ++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 2][1] - prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[dp.size() - 1][1];
}
};