题目:原题链接(中等)
标签:扫描线算法、排序
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
Ans 1 (Python) | 172ms (65.67%) | ||
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution:
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
slots1.sort()
slots2.sort()
s1, s2 = len(slots1), len(slots2)
i1, i2 = 0, 0
while i1 < s1 and i2 < s2:
start = max(slots1[i1][0], slots2[i2][0])
end = min(slots1[i1][1], slots2[i2][1])
if end - start >= duration:
return [start, start + duration]
else:
if slots1[i1][1] < slots2[i2][1]:
i1 += 1
else:
i2 += 1
return []