사용자 도구

사이트 도구


ps:problems:programmers:92341

주차 요금 계산

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

구현

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

풀이

  • 그냥 시키는대로 구현하면 되는 문제.
  • 자동차별로 주차 시간을 저장하는 것을 dict를 이용해서 처리하고, 자동차 번호순으로 출력하기 위해서 소팅을 사용했기 때문에 시간복잡도가 O(nlogn)이 되었다. 자동차 번호의 범위가 0000~9999 까지이므로 그냥 크기 10000짜리 배열을 이용해서 처리한다면 소팅없이 O(10000) 의 상수시간으로 주차 요금을 번호순으로 출력하는 것도 가능하다.

코드

"""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

토론

댓글을 입력하세요:
B X H Q M
 
ps/problems/programmers/92341.txt · 마지막으로 수정됨: 2022/02/28 07:21 저자 teferi