买卖股票

1. 剑指offer63股票的最大利润

剑指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 买卖股票的最佳时机

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

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

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

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 最佳买卖股票时机含冷冻期

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 买卖股票的最佳时机含手续费

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;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值