首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【LeetCode教你买股票】买卖股票的最佳时机

【LeetCode教你买股票】买卖股票的最佳时机

作者头像
_小羊_
发布2025-05-28 08:31:15
发布2025-05-28 08:31:15
15100
代码可运行
举报
文章被收录于专栏:C++C++
运行总次数:0
代码可运行

买卖股票的最佳时机

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0, m = 1e4 + 1;
        for (auto e : prices)
        {
            m = min(m, e);
            res = max(res, e - m);
        }   
        return res;
    }
};

买卖股票的最佳时机 II

动态规划解法。

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<int> dp(2), tmp(2);
        dp[0] = -prices[0];
        for (int i = 1; i < n; i++)
        {
            tmp[0] = max(dp[0], dp[1] - prices[i]);
            tmp[1] = max(dp[1], dp[0] + prices[i]);
            dp[0] = tmp[0];
            dp[1] = tmp[1];
        }
        return dp[1];
    }
};

贪心解法。

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        for (int i = 0; i < prices.size() - 1; i++)
        {
            res += max(0, prices[i + 1] - prices[i]);
        }
        return res;
    }
};

买卖股票的最佳时机 III

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        const int N = 0x3f3f3f3f;
        vector<int> f(3, -N);
        auto g = f;
        f[0] = -prices[0];
        g[0] = 0;
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                f[j] = max(f[j], g[j] - prices[i]);
                if (j > 0) g[j] = max(g[j], f[j - 1] + prices[i]);
            }
        }
        int res = 0;
        for (auto e : g) 
        {
            res = max(res, e);
        }
        return res;
    }
};

买卖股票的最佳时机 IV

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        int n = prices.size();
        const int N = 0x3f3f3f3f;
        vector<int> f(k + 1, -N);
        auto g = f;
        f[0] = -prices[0];
        g[0] = 0;
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j <= k; j++)
            {
                f[j] = max(f[j], g[j] - prices[i]);
                if (j > 0) g[j] = max(g[j], f[j - 1] + prices[i]);
            }
        }
        int res = 0;
        for (auto e : g) res = max(res, e);
        return res;
    }
};

买卖股票的最佳时机含冷冻期

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<int> dp(3), tmp(3);
        dp[0] = -prices[0];
        for (int i = 1; i < n; i++)
        {
            tmp[0] = max(dp[0], dp[1] - prices[i]);
            tmp[1] = max(dp[1], dp[2]);
            tmp[2] = dp[0] + prices[i];
            dp[0] = tmp[0];
            dp[1] = tmp[1];
            dp[2] = tmp[2];
        }
        return max(dp[1], dp[2]);
    }
};

买卖股票的最佳时机含手续费

代码语言:javascript
代码运行次数:0
运行
复制
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<int> dp(2), tmp(2);
        dp[0] = -prices[0];
        for (int i = 1; i < n; i++)
        {
            tmp[0] = max(dp[0], dp[1] - prices[i]);
            tmp[1] = max(dp[1], dp[0] + prices[i] - fee);
            dp[0] = tmp[0];
            dp[1] = tmp[1];
        }
        return dp[1];
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-05-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 买卖股票的最佳时机
  • 买卖股票的最佳时机 II
  • 买卖股票的最佳时机 III
  • 买卖股票的最佳时机 IV
  • 买卖股票的最佳时机含冷冻期
  • 买卖股票的最佳时机含手续费
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档