力扣hot100 -- 贪心算法

👂 ▶ 逍遥叹 - 胡歌&沈以城【Mashup】 (163.com)

👂 庐州月 - 许嵩 - 单曲 - 网易云音乐

2.7 小时,加上写博客,4 道题,😂 -- 希望二刷时,可以 3 小时,8 道题....

目录

🍉买卖股票的最佳时机

🌼跳跃游戏

[🍓跳跃游戏 II](#🍓跳跃游戏 II)

🎂划分字母区间


🍉买卖股票的最佳时机

121. 买卖股票的最佳时机 - 力扣(LeetCode)

贪心:维护目前为止的最小值

cpp 复制代码
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minPrice = 1e4; // 维护一个最小值
        int ans = 0;
        for (auto x : prices) {
            minPrice = min(minPrice, x);
            ans = max(ans, x - minPrice);
        }
        ans = ans < 0 ? 0 : ans;
        return ans;
    }
};

🌼跳跃游戏

55. 跳跃游戏 - 力扣(LeetCode)

贪心:维护可以跳到的最大索引(下标)

cpp 复制代码
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int Max = 0; // 维护当前可以跳到的最远下标
        for (int i = 0; i < nums.size(); ++i) {
            // 当前索引 i > Max, 说明无法到达
            if (i > Max)
                return false;
            if (Max >= nums.size() - 1)
                break;
            Max = max(Max, i + nums[i]);
        }
        return true;
    }
};

// 1 2 5 1 1 0 3 1 0  1 1 1

🍓跳跃游戏 II

45. 跳跃游戏 II - 力扣(LeetCode)

题目保证 "可以到达 numsn - 1"

每次跳跃前,先计算最大范围,然后依次计算这个范围内

各个坐标所能到达的最远的点 maxPos,作为本次跳跃的目的地 end....

cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        int maxPos = 0; // 维护最远的点
        int ans = 0, end = 0;

        // 不是 < nums.size(), 最后一个元素不用继续跳了
        for (int i = 0; i < nums.size() - 1; ++i) { 
            // 最大范围 [i, end]
            maxPos = max(maxPos, i + nums[i]); // 维护最远距离
            if (i == end) { // 这一跳结束
                end = maxPos;
                ans++; // 只有到达 end, 才更新步数
            }
        }
        return ans;
    }
};

// 1 1
// 1

🎂划分字母区间

763. 划分字母区间 - 力扣(LeetCode)

第一次遍历 O(n),用一个数组 last26 维护每个字母最后出现的位置
当前子串范围 start, end

第 2 次遍历 O(n),逐个遍历 i

动态地用 lasts\[i - 'a'] 更新 end,直到 i == end,满足当前子串要求

cpp 复制代码
class Solution {
public:
    vector<int> partitionLabels(string s) {
        vector<int> ans;
        int last[26];

        for (int i = 0; i < s.size(); ++i)
            last[s[i] - 'a'] = i; // 字母 s[i] 最后出现位置

        int start = 0, end = 0; // 当前划分范围
        for (int i = 0; i < s.size(); ++i) {
            end = max(end, last[s[i] - 'a']); // 更新最后出现位置
            if (i == end) {
                ans.push_back(end - start + 1);
                start = end + 1;
            }
        }
        return ans;
    }
};
相关推荐
大明者省10 小时前
WSL2 Ubuntu22.04 GPU训练环境配置指南
人工智能·算法·计算机视觉
白狐_79811 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander25812 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
Shell运维手记12 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
杨航 AI12 小时前
O(n log n):线性对数原理拆解 这个是排序算法的黄金复杂度之一。
算法·排序算法
船厂电气自动化ai大模型13 小时前
AI大模型与数学 第32课 函数凹凸性与二阶导数:拐点求解、凹凸区间计算(10道二阶导数计算题)
数据结构·人工智能·python·深度学习·算法
旖旎夜光13 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
叠层归一研究院14 小时前
AGI 系统(十一):二阶网络 — 二阶递归 × 多主体 (元符号网络)
开发语言·人工智能·算法·php·agi
zlinear数据采集卡15 小时前
D223的PWM电机控制:6路独立脉冲+加减速算法深度解析
arm开发·stm32·嵌入式硬件·算法·fpga开发·架构
杨石兴15 小时前
31、玩具MoM:用2x2矩阵串起全套流程
人工智能·算法·矩阵·电磁学