代码随想录day32

一旦有重叠区域,则用min更新右边界

python 复制代码
class Solution(object):
    def findMinArrowShots(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        points.sort(key=lambda x:x[0])
        if len(points)==0:
            return 0

        count = 1
        
        for i in range(1, len(points)):
            if points[i][0] > points[i-1][1]:
                count += 1
            else:
                points[i][1] = min(points[i-1][1],points[i][1])
                
        return count

跟上题一模一样

python 复制代码
class Solution(object):
    def eraseOverlapIntervals(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: int
        """
        intervals.sort(key=lambda x:x[0])
        if len(intervals)==0:
            return 0

        count = 0
        
        for i in range(1, len(intervals)):
            if intervals[i][0] < intervals[i-1][1]:
                count += 1
                intervals[i][1] = min(intervals[i-1][1],intervals[i][1])

                
        return count

第三题

构建哈希表,记录每个字母最大位置缩影,重新遍历,不断更新最大位置索引

python 复制代码
class Solution(object):
    def partitionLabels(self, s):
        """
        :type s: str
        :rtype: List[int]
        """
        hashmap = {}
        result = [] 
        for i in range(len(s)):
            hashmap[s[i]] = i

        left,right = 0, 0
        
        for i in range(len(s)):
            right = max(hashmap[s[i]],right) #不断更新最大位置索引,直到i==最大位置索引
            if right == i:
                result.append(right-left+1)#返回长度
                left = i + 1
        return result
相关推荐
Benny_Tang3 分钟前
「雅礼集训 2018 Day7」A 题解
数据结构·c++·算法
Doubbbbbbble云13 分钟前
跳表结构在高并发系统中的应用与优势分析4
算法
TAN-90°-14 分钟前
Deep Learning for Computer Vision——Large Scale Distributed Training
人工智能·深度学习·神经网络·算法·机器学习·计算机视觉·语言模型
Lyyaoo.24 分钟前
【回溯】【中等】全排列
java·数据结构·算法
写后端的胖头鱼28 分钟前
一文讲懂JVM与调优
jvm·后端·算法·架构·jvm调优
土司大王36 分钟前
LeetCode 79 单词搜索:Java 回溯模板、网格 DFS 与剪枝优化
java·算法·leetcode·深度优先
ECT-OS-JiuHuaShan41 分钟前
哲学是迭代学,数学是拓扑学
开发语言·人工智能·学习·算法·机器学习·php·拓扑学
All for pursuit.43 分钟前
【数组-5】560.和为K的子数组
数据结构·c++·算法·leetcode
Beyond_System|系统之外1 小时前
【学编程】Python基础编程题100道(1-20)
java·数据结构·算法
维克兜率天1 小时前
【维克】一个极端值,毁掉了整个因子
大数据·人工智能·python·算法·机器学习