代码随想录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
相关推荐
每天努力进步!9 分钟前
LeetCode热题100刷题6:160. 相交链表、206. 反转链表、234. 回文链表、141. 环形链表、142. 环形链表 II
c++·算法·leetcode·链表
weixin_4327027618 分钟前
统计信号处理基础 习题解答11-1
算法·信号处理
爱编程的Tom27 分钟前
Map && Set(Java篇详解)
java·开发语言·数据结构·学习·算法
JokerSZ.2 小时前
【Leetcode 每日一题】268. 丢失的数字
数据结构·算法·leetcode
㣲信团队2 小时前
小和问题和逆序对问题
数据结构·算法
muyierfly2 小时前
DAY19-力扣刷题
数据结构·算法·leetcode
小梁不秃捏3 小时前
科比老大职业生涯数据预测(基于随机森林模型)
算法·随机森林·机器学习
科技之歌3 小时前
Leetcode 115 不同的子序列
算法·leetcode·职场和发展
斯择微韵3 小时前
力扣习题--哈沙德数
算法·leetcode·职场和发展