代码随想录day36|435. 无重叠区间763. 划分字母区间56. 合并区间

435. 无重叠区间

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        if len(intervals) == 0:
            return 0
        result = 0
        intervals = sorted(intervals,key=lambda x:x[0])
        for i in range(1,len(intervals)):
            if intervals[i][0] < intervals[i-1][1]:
                result+=1
                intervals[i][1] = min(intervals[i][1],intervals[i-1][1])
        return result

763. 划分字母区间

class Solution:
    def partitionLabels(self, s: str) -> List[int]:
        hash_ = [0]*27
        result = []
        left = 0 
        right = 0
        for i in range(len(s)):
            hash_[ord(s[i])-ord('a')] =i
        for i in range(len(s)):
            right = max(right, hash_[ord(s[i])-ord('a')])
            if i == right:
                result.append(right-left+1)
                left = i+1   
        return result

56. 合并区间

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

你可能感兴趣的:(python,leetcode)