代码随想录算法训练营第35天|● 435. 无重叠区间 ● 763.划分字母区间 ● 56. 合并区间

435. 无重叠区间

和之前的是一样的,更新一个最短区间

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        # 按左区间排序 算重叠的区间就是要删除的区间
        if not intervals:return 0
        intervals.sort(key=lambda x:x[0])
        cnt=0
        for i in range(1,len(intervals)):
            if intervals[i][0]<intervals[i-1][1]:
                cnt+=1
                intervals[i][1]=min(intervals[i-1][1],intervals[i][1])
        return cnt


 763. 划分字母区间

先得到每个字母的最后出现位置 然后能划分就划分

class Solution:
    def partitionLabels(self, s: str) -> List[int]:
        lastch={}
        for i,ch in enumerate(s):
            lastch[ch]=i
        res=[]
        start,end=0,0
        for i,ch in enumerate(s):
            end=max(end,lastch[ch])
            if i==end:
                res.append(end-start+1)
                start=i+1
        return res

56. 合并区间 

搞一个list 更新尾巴

class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        res=[]
        if not intervals:return res
        intervals.sort(key=lambda x:x[0])
        res.append(intervals[0])
        for i in range(1,len(intervals)):
            if res[-1][1]>=intervals[i][0]:
                res[-1][1]=max(res[-1][1],intervals[i][1])
            else:
                res.append(intervals[i])
        return res

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-14 16:30:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-14 16:30:08       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-14 16:30:08       82 阅读
  4. Python语言-面向对象

    2024-05-14 16:30:08       91 阅读

热门阅读

  1. Kubernetes(k8s)的Network Policies解析

    2024-05-14 16:30:08       27 阅读
  2. 实用的chrome命令

    2024-05-14 16:30:08       25 阅读
  3. js通过视频链接获取视频时长

    2024-05-14 16:30:08       38 阅读
  4. Python实战开发及案例分析(20)—— 宽度优先

    2024-05-14 16:30:08       35 阅读
  5. 【前端每日一题】day5

    2024-05-14 16:30:08       37 阅读
  6. GNU/Linux - 是否可以多次打开同一个设备文件

    2024-05-14 16:30:08       29 阅读
  7. LeetCode-hot100题解—Day7

    2024-05-14 16:30:08       36 阅读
  8. 机器学习【简述】

    2024-05-14 16:30:08       31 阅读
  9. 【TypeScript声明合并简介以及使用方法】

    2024-05-14 16:30:08       39 阅读
  10. 【C++】字符串出现次数

    2024-05-14 16:30:08       31 阅读
  11. Mysql 锁

    Mysql 锁

    2024-05-14 16:30:08      36 阅读