✅DAY30 贪心算法 | 452. 用最少数量的箭引爆气球 | 435. 无重叠区间 | 763.划分字母区间

452. 用最少数量的箭引爆气球

解题思路:首先把原数组按左边界进行排序。然后比较[i-1]的右边界和[i]的左边界是否重叠,如果重叠,更新当前右边界为最小右边界和[i+1]的左边界判断是重叠。

python 复制代码
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if len(points) == 0: return 0
        points.sort(key=lambda x: x[0])
        result = 1
        for i in range(1, len(points)):
            if points[i][0] > points[i-1][1]: # 第二段的头和第一段的尾不衔接
                result += 1
            else:
                points[i][1] = min(points[i-1][1], points[i][1]) 
                # else是这两段重叠,但是要判断下一个部分是否重叠
                # 更新尾为最小的右边界,如果下一个[i][0]< [i-1][1],则又有一段重叠
        return result

优化:按右边界排序的方式通常更直观,因为只需要维护一个变量 current_end 表示当前的射击位置,不需要更新区间边界,也减少了不必要的操作。

python 复制代码
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if not points: return 0
        points.sort(key=lambda x: x[1])  # 按右边界排序
        result = 1
        current_end = points[0][1]
        for i in range(1, len(points)):
            if points[i][0] > current_end:
                result += 1
                current_end = points[i][1]
        return result

435. 无重叠区间

python 复制代码
class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        if not intervals: return 0

        intervals.sort(key = lambda x:x[0])
        count = 0

        for i in range(1, len(intervals)):
            if intervals[i-1][1] > intervals[i][0]: # 存在重叠
                intervals[i][1] = min(intervals[i-1][1], intervals[i][1])
                count += 1
        return count

763. 划分字母区间

python 复制代码
class Solution:
    def partitionLabels(self, s: str) -> List[int]:
        last_occ = {}
        result = []
        for i, char in enumerate(s):
            last_occ[char] = i
        start_index, end_index = 0, 0
        for i, char in enumerate(s):
            end_index = max(end_index, last_occ[char])#找出当前字符出现的最远位置
            if i == end_index:#i遍历到当前end的最尾部,说明前面的所有出现字符到这停下了
                result.append(end_index - start_index + 1)
                start_index = i + 1
        return result
相关推荐
冷雨夜中漫步6 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴6 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再6 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
颜酱7 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
m0_736919108 小时前
C++代码风格检查工具
开发语言·c++·算法
yugi9878388 小时前
基于MATLAB强化学习的单智能体与多智能体路径规划算法
算法·matlab
喵手8 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934738 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy8 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
DuHz8 小时前
超宽带脉冲无线电(Ultra Wideband Impulse Radio, UWB)简介
论文阅读·算法·汽车·信息与通信·信号处理