input으로 주어진 두 스케쥴을 첫번째 요소의 크기를 기준으로 오름차순으로 정렬 후 각각에 index를 부여했다. 시작 시간은 비교 중인 두 slot 중 큰 값을, 끝나는 시간은 작은 값을 계산해 공통되는 구간을 찾고 해당 구간의 크기가 duration보다 길면 충분히 겹치는 시간이 있기에 반환한다. 스케쥴의 각 slot에서 두번째 요소인 끝나는 시간을 기준으로 index를 증가시켜 나갔다.
class Solution:
def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
slots1.sort()
slots2.sort()
i = j = 0
while i < len(slots1) and j < len(slots2):
if slots1[i][0] < slots2[j][1] or slots1[i][1] > slots2[j][0]:
s = max(slots1[i][0], slots2[j][0])
e = min(slots1[i][1], slots2[j][1])
if e - s >= duration:
return [s, s + duration]
if slots1[i][1] < slots2[j][1]:
i += 1
else:
j += 1
return []
'기술(Tech, IT) > 리트코드(LeetCode)' 카테고리의 다른 글
[LeetCode] 50. Pow(x, n) (0) | 2023.08.05 |
---|---|
[LeetCode] 1244. Design A Leaderboard (0) | 2023.08.02 |
[LeetCode] 1214. Two Sum BSTs (0) | 2023.07.31 |
[LeetCode] 2026. Low-Quality Problems (0) | 2023.07.30 |
[LeetCode] 2520. Count the Digits That Divide a Number (0) | 2023.07.29 |