一、刷题:
1.leetcode题目 435. 无重叠区间 - 力扣(LeetCode)(medium)
解决:
python
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda x:(x[0],x[1]))
ans = 0
for i in range(1,len(intervals)):
if intervals[i][0]<intervals[i-1][1]:
ans += 1
intervals[i][1] = min(intervals[i-1][1],intervals[i][1])
return ans
2.leetcode题目 763. 划分字母区间 - 力扣(LeetCode)(medium)
解决:
python
class Solution:
def partitionLabels(self, s: str) -> List[int]:
last = {}
for i,ch in enumerate(s):
last[ch] = i
start =0
ans = []
end = 0
for i,ch in enumerate(s):
end = max(end,last[ch])
if i==end:
length = end-start+1
ans.append(length)
start = i+1
return ans
3.leetcode题目 56. 合并区间 - 力扣(LeetCode)(medium)
解决:
python
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key = lambda x:(x[0],x[1]))
ans = [intervals[0]]
for i in range(1,len(intervals)):
if intervals[i][0] <= intervals[i-1][1]:
ans[-1] = [intervals[i-1][0],max(intervals[i][1],intervals[i-1][1])]
intervals[i] = [intervals[i-1][0],max(intervals[i][1],intervals[i-1][1])]
else:
ans.append(intervals[i])
return ans