435. 无重叠区间
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
count = 0
intervals.sort(key=lambda x: x[0])
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[i-1][1]:
intervals[i][1] = min(intervals[i][1], intervals[i-1][1])
count += 1
return count
763.划分字母区间
class Solution:
def partitionLabels(self, s: str) -> List[int]:
table = {}
start = 0
end = 0
result = []
for i in range(len(s)):
table[s[i]] = i
for i in range(len(s)):
end = max(end, table[s[i]])
if i == end:
result.append(end-start+1)
start = i+1
return result
56. 合并区间
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
if len(intervals) == 0:
return intervals
intervals.sort(key=lambda x: x[0])
result = [intervals[0]]
for i in range(1, len(intervals)):
if result[-1][1] >= intervals[i][0]:
result[-1][1] = max(intervals[i][1], result[-1][1])
else:
result.append(intervals[i])
return result
标签:end,int,随想录,List,len,算法,intervals,result,区间
From: https://www.cnblogs.com/miramira/p/18158317