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;
    }
};
相关推荐
ComputerInBook20 分钟前
C++编程语言:标准库:第37章——正则表达式(Bjarne Stroustrup)
开发语言·c++·正则表达式
青草地溪水旁33 分钟前
C/C++中的可变参数 (Variadic Arguments)函数机制
c语言·c++·可变参数
汉克老师1 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(1、求和)
c++·蓝桥杯·蓝桥杯c++·c++蓝桥杯
XXYBMOOO1 小时前
Qt UDP 通信类详解与实现
开发语言·网络·c++·qt·网络协议·ui·udp
君鼎1 小时前
More Effective C++ 条款29:引用计数
c++
圣保罗的大教堂1 小时前
leetcode 2749. 得到整数零需要执行的最少操作数 中等
leetcode
小欣加油1 小时前
leetcode 6 Z字形变化
c++·算法·leetcode·职场和发展
曙曙学编程1 小时前
stm32——寄存器操作,蜂鸣器原理
c语言·c++·stm32·单片机·嵌入式硬件
田里的水稻2 小时前
C++_数据类型和数据结构
java·数据结构·c++
notfindjob3 小时前
Opencv C++ 教程-人脸识别
c++·opencv·计算机视觉