买卖股票
1. 剑指offer63股票的最大利润
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len <= 1)
return 0;
int profit = 0;
int minPrice = prices[0];
for(int i = 1; i < len; ++i)
{
minPrice = min(minPrice, prices[i]);
profit = max(profit, prices[i] - minPrice);
}
return profit;
}
};
2. 121 买卖股票的最佳时机
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len <= 1)
return 0;
int profit = 0;
int minPrice = prices[0];
for(int i = 1; i < len; ++i)
{
minPrice = min(minPrice, prices[i]);
profit = max(profit, prices[i] - minPrice);
}
return profit;
}
};
3. 122 买卖股票的最佳时机 II
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
int dp[len][2];
dp[0][0] = 0, dp[0][1] = -prices[0]; // 初始化为负数,方便卖股票时计算
for(int i = 1; i < len; ++i)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[len - 1][0];
}
};
4. 123 买卖股票的最佳时机 III
class Solution {
public:
int maxProfit(vector<int>& prices) {
int firstbuy = INT_MIN, firstsell = 0;
int secondbuy = INT_MIN, secondsell = 0;
for(const auto& e : prices)
{
firstbuy = max(firstbuy, -e); // 第一次买
firstsell = max(firstsell, firstbuy + e); // 第一次买,然后卖
secondbuy = max(secondbuy, firstsell - e); // 第一次卖了后,现在买
secondsell = max(secondsell, secondbuy + e); // 第二次买了后,现在卖
}
return secondsell;
}
};
5. 188 买卖股票的最佳时机 IV
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int len = prices.size();
if(k == 0 || len <= 1)
return 0;
int dp[k][2]; // dp[k][0] : 卖了,dp[k][1] : 不卖
for(int i = 0; i < k; ++i) // 初始化
{
dp[i][0] = 0;
dp[i][1] = -prices[0];
}
for(int i = 1; i < len; ++i)
{
dp[0][1] = max(dp[0][1], -prices[i]);
dp[0][0] = max(dp[0][0], dp[0][1] + prices[i]);
for(int j = 1; j < k; ++j)
{
dp[j][1] = max(dp[j][1], dp[j - 1][0] - prices[i]);
dp[j][0] = max(dp[j][0], dp[j][1] + prices[i]);
}
}
return dp[k - 1][0];
}
};
6. 309 最佳买卖股票时机含冷冻期
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len <= 1)
return 0;
int dp[len][2];
dp[0][0] = 0, dp[0][1] = -prices[0];
dp[1][0] = max(dp[0][0], dp[0][1] + prices[1]);
dp[1][1] = max(dp[0][1], -prices[1]);
for(int i = 2; i < len; ++i)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 2][0] - prices[i]);
}
return dp[len - 1][0];
}
};
7. 714 买卖股票的最佳时机含手续费
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int len = prices.size();
if(len <= 1)
return 0;
int dp[len][2];
dp[0][0] = 0, dp[0][1] = -prices[0];
for(int i = 1; i < len; ++i)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[len - 1][0];
}
};
8. 901 股票价格跨度
901 股票价格跨度
// 单调栈
class StockSpanner {
public:
StockSpanner() {}
int next(int price) {
int res = 1;
while(!st.empty() && price >= st.top().first)
{
res += st.top().second;
st.pop();
}
st.push({price, res});
return res;
}
private:
stack<pair<int, int>> st;
};