435. no overlap interval 452 detonating the balloon with the minimum number of arrows

Ideas:
may refer to: greedy algorithm of interval scheduling problem

435. no overlap interval

This figure is easy to understand.
The time complexity of O (nlogn)
space complexity of O (1)
Here Insert Picture Description

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        n = len(intervals) # 总数
        if n<=1:return 0
        intervals.sort(key=lambda x:x[1]) # 安照终点排序,优先保留终点小的,这样保留的集合最多,删除的集合最少
        count = 1
        x_end = intervals[0][1] # 第一个数的终点
        for val in intervals:
            if val[0]>=x_end:   # 如果后面的起点大于等于上一个的终点,则再次计数
                count+=1
                x_end = val[1]
        return n-count

Here Insert Picture Description

452. detonated a balloon with a minimal number of arrows

The time complexity of O (nlogn)
space complexity of O (1)

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        intervals = points  # 修改输入
        n = len(intervals)  
        if n<1:return 0     # 不要等号
        intervals.sort(key=lambda x:x[1])  
        count = 1
        x_end = intervals[0][1]  
        for val in intervals:
            if val[0]> x_end:    # 不要等号
                count+=1
                x_end = val[1]
        return count  # 直接输出

Here Insert Picture Description

Published 135 original articles · won praise 5 · Views 7098

Guess you like

Origin blog.csdn.net/qq_27921205/article/details/104418786
Recommended