문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42884
해당 문제는 범위 계산 문제이다.
최초로 푼 소스 코드
from typing import List
from collections import deque
def solution(routes: List[List[int]]):
answer = 0
routes.sort(key = lambda x: (x[0], x[1]))
q = deque(routes)
while q:
answer += 1
cur_start, cur_end = q.popleft()
while q:
next_start, next_end = q[0]
if cur_end < next_start:
break
else:
cur_end = min(cur_end, next_end)
q.popleft()
return answer
개선한 코드
from typing import List
def solution(routes: List[List[int]]):
answer = 1
routes.sort(key = lambda x: (x[0], x[1]))
cur_start, cur_end = routes.pop(0)
for route in routes:
next_start, next_end = route
if next_start <= cur_end:
cur_end = min(cur_end, next_end)
else:
answer += 1
cur_end = next_end
return answer