기술(Tech, IT)/리트코드(LeetCode)
[LeetCode] 1229. Meeting Scheduler
Daniel803
2023. 8. 1. 07:56
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 []
반응형