Python 代码
按照左端点进行排序,然后逐步合并
nums = 0
n = int(input())
intervals = []
for i in range(n):
l,r = map(int,input().split())
intervals.append([l,r])
intervals = sorted(intervals, key=lambda x:x[0])
i = 0
while i < n:
temp = intervals[i]
while i < n and intervals[i][0] <= temp[1]:
temp[1] = max(temp[1], intervals[i][1])
i += 1
nums += 1
print(nums)