본문 바로가기
코딩테스트/Python

[python] 프로그래머스 호텔 대실

by nahkim 2023. 4. 16.

📜 접근 방법

  1. 시작 시간을 기준으로 오름차순을 한다.
  2. 리스트에 끝나는 시간을 넣는다.
  3. 시작시간과 끝나는 시간을 비교하여 시작시간이 끝나는 시간 + 10분보다 작으면 객실 수를 추가한다.

비슷한 문제 : https://www.acmicpc.net/problem/1374

 

❌ 실패 코드

문자열을 시간으로 바꾸는 것에서 문제가 생겼다.

 

✅ 정답 코드

from heapq import heappush, heappop

def solution(book_time):
    answer = 0
    heap = []
    time_list = []
    for s, e in book_time:
        time_list.append([int(s[:2]) * 60 + int(s[3:]), int(e[:2]) * 60 + int(e[3:])])
    time_list.sort(key=lambda x:x[0])

    for time in time_list:
        heappush(heap, time[1])

    for time in time_list:
        s = time[0]
        e = heappop(heap)
        if s < e + 10:
            answer += 1
            heappush(heap, e)
    return answer

 

💡알게 된점

시간을 자체로 사용하려하지말고 바꾸려는 생각을 좀 해야할 것 같다.