代码随想录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
相关推荐
你怎么知道我是队长3 分钟前
C语言---循环结构
c语言·开发语言·算法
艾醒6 分钟前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法
纪元A梦2 小时前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
_不会dp不改名_3 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
mark-puls3 小时前
C语言打印爱心
c语言·开发语言·算法
Python技术极客3 小时前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法
睡不醒的kun3 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌3 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
我星期八休息4 小时前
深入理解跳表(Skip List):原理、实现与应用
开发语言·数据结构·人工智能·python·算法·list
lingran__4 小时前
速通ACM省铜第四天 赋源码(G-C-D, Unlucky!)
c++·算法