代码随想录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
相关推荐
music&movie37 分钟前
算法工程师认知水平要求总结
人工智能·算法
laocui11 小时前
Σ∆ 数字滤波
人工智能·算法
yzx9910132 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
全栈凯哥2 小时前
Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解
java·算法·leetcode·链表
全栈凯哥2 小时前
Java详解LeetCode 热题 100(27):LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)详解
java·算法·leetcode·链表
SuperCandyXu2 小时前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
Humbunklung3 小时前
机器学习算法分类
算法·机器学习·分类
Ai多利3 小时前
深度学习登上Nature子刊!特征选择创新思路
人工智能·算法·计算机视觉·多模态·特征选择
Q8137574604 小时前
中阳视角下的资产配置趋势分析与算法支持
算法
yvestine4 小时前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示