목차

주차 요금 계산

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호92341
문제명주차 요금 계산
레벨Level 2
분류

구현

시간복잡도O(nlogn)
인풋사이즈n<=1000
사용한 언어Python
해결날짜2022/02/28

풀이

코드

"""Solution code for "Programmers 92341. 주차 요금 계산".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/92341
- Solution link: http://www.teferi.net/ps/problems/programmers/92341
"""

import collections
import math


def solution(fees, records):
    enter_time = {}
    park_time = collections.defaultdict(int)
    for record in records:
        time_str, car, _ = record.split()
        hh, mm = [int(x) for x in time_str.split(':')]
        time_in_minute = hh * 60 + mm
        if (t := enter_time.get(car)) is None:
            enter_time[car] = time_in_minute
        else:
            park_time[car] += time_in_minute - t
            del enter_time[car]
    for car, t in enter_time.items():
        park_time[car] += 23 * 60 + 59 - t

    base_time, base_fee, unit_time, unit_fee = fees
    answer = []
    for _, t in sorted(park_time.items()):
        if t <= base_time:
            fee = base_fee
        else:
            fee = base_fee + math.ceil((t - base_time) / unit_time) * unit_fee
        answer.append(fee)

    return answer