Lv3 프로그래머스(Programmers)[Python][파이썬] 단속 카메라

"""
출처:프로그래머스
https://school.programmers.co.kr/learn/courses/30/lessons/42884
"""

# 풀이 과정
from collections import defaultdict
from collections import deque
import heapq


def solution(routes):
check = []
car_start = defaultdict(set)
car_end = defaultdict(set)
for c in range(len(routes)):
check.append(routes[c][0])
check.append(routes[c][1])

car_start[routes[c][0]].add(c)
car_end[routes[c][1]].add(c)

start = min(check)
end = max(check)
check.sort()

now = set([])

result = 0

check_car = set([])

# 이전에 발견되던게 지금 발견 안되면 그 지점은 무조건 카메라 설치 필수
for time in check:
now = now | car_start[time]

# 빠지는 차량 발생
if len(car_end[time]) > 0:
if len(car_end[time] - check_car) > 0:
result += 1
check_car = check_car | car_end[time] | now

return result