본문 바로가기
프로그래머스/lv.2

주차 요금 계산

by bingual 2024. 2. 16.
반응형

 

https://school.programmers.co.kr/learn/courses/30/lessons/92341

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제풀이

from math import ceil
from collections import defaultdict


def solution(fees, records):
    history = {}
    remainder = defaultdict(int)
    fee = {}
    d_time, d_fee, u_time, u_fee = fees

    for item in records:
        time, num, check = item.split()
        if check == "IN":
            history[num] = convert(time)
        else:
            remainder[num] += abs(convert(time) - history[num])
            history.pop(num)

    if history:
        for num, time in history.items():
            remainder[num] += abs(time - 1439)

    for num, time in remainder.items():
        if time > d_time:
            fee[num] = d_fee + ceil((time - d_time) / u_time) * u_fee
        else:
            fee[num] = d_fee

    return [y for x, y in sorted(fee.items(), key=lambda x: x[0])]


def convert(time):
    h, m = time.split(":")
    return int(h) * 60 + int(m)

'프로그래머스 > lv.2' 카테고리의 다른 글

주식가격  (0) 2024.02.17
방문 길이  (0) 2024.02.17
뒤에 있는 큰 수 찾기  (1) 2024.02.16
더 맵게  (0) 2024.02.16
압축  (0) 2024.02.16