LeetCode Hot 100:贪心算法

LeetCode Hot 100:贪心算法

121. 买卖股票的最佳时机

cpp 复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minPrice = INT_MAX;
        int maxProfit = 0;
        for (int& price : prices) {
            minPrice = min(minPrice, price);
            maxProfit = max(maxProfit, price - minPrice);
        }

        return maxProfit;
    }
};

思路 1:动态规划

cpp 复制代码
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int n = nums.size();
        // dp[n] 表示从下标范围 [0,i] 中的任意下标出发可以到达的最大下标
        vector<int> dp(n);
        // 初始化
        dp[0] = nums[0];
        // 状态转移
        for (int i = 1; i < n; i++) {
            if (dp[i - 1] < i)
                return false;
            dp[i] = max(dp[i - 1], i + nums[i]);
        }
        return dp[n - 1] >= n - 1;
    }
};

思路 2:贪心

cpp 复制代码
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int n = nums.size();
        int max_far = 0; // 目前能跳到的最远位置

        for (int i = 0; i < n; i++) {
            if (i <= max_far) {
                max_far = max(max_far, i + nums[i]);
                if (max_far >= n - 1)
                    return true;
            }
        }

        return false;
    }
};

45. 跳跃游戏 II

思路 1:贪心

cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        int n = nums.size();
        int pos = n - 1, step = 0;

        while (pos > 0) {
            for (int i = 0; i < pos; i++)
                if (i + nums[i] >= pos) {
                    pos = i;
                    step++;
                    break;
                }
        }
        
        return step;
    }
};

思路 2:动态规划

cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        int n = nums.size();
        // dp[i] 表示到达 i 的最少跳数
        vector<int> dp(n, INT_MAX);
        // 初始化
        dp[0] = 0;
        // 状态转移
        for (int i = 1, last = 0; i < n; i++) {
            while (last < n && last + nums[last] < i)
                last++;
            dp[i] = min(dp[i], dp[last] + 1);
        }

        return dp[n - 1];
    }
};

763. 划分字母区间

cpp 复制代码
class Solution {
public:
    vector<int> partitionLabels(string s) {
        int n = s.length();
        vector<int> last(26, 0);
        for (int i = 0; i < n; i++)
            last[s[i] - 'a'] = i;

        vector<int> ans;
        int start = 0, end = 0;
        for (int i = 0; i < n; i++) {
            end = max(end, last[s[i] - 'a']);
            if (i == end) {
                ans.push_back(end - start + 1);
                start = end + 1;
            }
        }

        return ans;
    }
};
相关推荐
你的冰西瓜2 分钟前
C++ 中最短路算法的详细介绍
c++·算法·图论·最短路
zstar-_8 分钟前
【算法笔记】6.LeetCode-Hot100-链表专项
笔记·算法·leetcode
<但凡.16 分钟前
数据结构与算法之美:广义表
数据结构·c++·算法
偷偷的卷2 小时前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
大白的编程日记.2 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
C语言小火车2 小时前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针
凤年徐2 小时前
【数据结构】时间复杂度和空间复杂度
c语言·数据结构·c++·笔记·算法
踏莎行hyx3 小时前
使用langchain连接llama.cpp部署的本地deepseek大模型开发简单的LLM应用
c++·ai·langchain·大模型·llama.cpp·deepseek
山河木马3 小时前
前端学C++可太简单了:双冒号 :: 操作符
前端·javascript·c++
乌萨奇也要立志学C++4 小时前
【C++详解】STL-list模拟实现(深度剖析list迭代器,类模板未实例化取嵌套类型问题)
c++·list