代码随想录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
相关推荐
JackieZhang.2 分钟前
求水仙花数,提取算好,打表法。或者暴力解出来。
数据结构·算法
LUCIAZZZ29 分钟前
Hot100之贪心算法
数据结构·算法·leetcode·贪心算法
h^hh44 分钟前
堆的模拟实现(详解)c++
数据结构·c++·算法
tt5555555555551 小时前
每日一题——小根堆实现堆排序算法
c语言·数据结构·算法·面试·排序算法·八股文
亦梦亦醒乐逍遥3 小时前
【C++基础】字符串/字符读取函数解析
java·c++·算法
捞鱼哲学家3 小时前
【hot100】刷题记录(11)-搜索二维矩阵 II
数据结构·线性代数·算法·leetcode·矩阵
L_M_TY3 小时前
G1. Yunli‘s Subarray Queries (easy version)
算法·stl·滑动窗口·离线查询
查理零世4 小时前
【算法】回溯算法专题③ ——排列型回溯 python
python·算法·深度优先
利刃大大4 小时前
【数据结构与算法】九大排序算法实现详解
c语言·数据结构·c++·算法·排序算法
眼镜哥(with glasses)4 小时前
蓝桥杯python基础算法(2-1)——排序
数据结构·算法·蓝桥杯