代码随想录算法训练营DAY36补| 435. 无重叠区间、 763.划分字母区间

训练营计划更新了,补一下DAY36多出来的两道题

435. 无重叠区间

python 复制代码
class Solution(object):
    def eraseOverlapIntervals(self, intervals):
        """
        :type intervals: List[List[int]]
        :rtype: int
        """
        if not intervals:
            return 0

        intervals.sort(key=lambda x:x[0])
        result = 0

        for i in range(len(intervals)-1):
            if intervals[i][1]>intervals[i+1][0]:
                intervals[i+1][1] = min(intervals[i+1][1], intervals[i][1])
                result+=1

        return result

763.划分字母区间

python 复制代码
class Solution(object):
    def partitionLabels(self, s):
        """
        :type s: str
        :rtype: List[int]
        """
        last_occur = {}
        results = []

        start = 0
        end = 0

        for i, ch in enumerate(s):
            last_occur[ch]=i

        for i, ch in enumerate(s):
            end = max(end, last_occur[ch])
            if i==end:
                results.append(end-start+1)
                start = i+1
        
        return results
相关推荐
直接冲冲冲35 分钟前
数据结构-第八章(2.内部排序算法的比较及应用)
数据结构·算法·排序算法
笨笨胡小巴36 分钟前
贪心算法题目总结
算法
施霁36 分钟前
贪心算法——加工木棍(C++)
c++·算法·贪心算法
LeoLei80601 小时前
LeetCode.68文本左右对齐
c++·算法·leetcode
程小k1 小时前
【数据结构初阶】--- 归并排序
c语言·数据结构·c++·算法·排序算法
一直学习永不止步1 小时前
LeetCode题练习与总结:二叉树的后序遍历--145
java·算法·leetcode·二叉树···深度优先搜索
奔跑的玖伍2 小时前
数据结构 —— 二叉树
c语言·数据结构·算法·链表
太湖鹏哥2 小时前
6.2、函数的定义
开发语言·c++·算法
凯子坚持 c2 小时前
C语言----文件操作
c语言·开发语言·算法
酷酷学!!!2 小时前
归并排序-MergeSort (C语言详解)
c语言·算法·排序算法