주차 요금 계산

최민수·2023년 3월 17일
0

알고리즘

목록 보기
38/94
from collections import defaultdict
import math

def solution(fees, records):
    answer = []
    statusDict, timeDict = defaultdict(lambda: False), defaultdict(lambda: 0)
    resultDict = defaultdict(lambda: 0)

    for record in records:
        time, carNum, status = record.split(" ")
        hr, m = map(int, time.split(":"))
        getTime = hr*60 + m

        # IN
        if statusDict[carNum] == False and status == "IN":
            statusDict[carNum] = True
            timeDict[carNum] += getTime
        # OUT
        elif statusDict[carNum] and status == "OUT":
            statusDict[carNum] = False
            timeDiff = getTime - timeDict[carNum]            
            resultDict[carNum] += timeDiff
            timeDict[carNum] = 0

    # IN, 하지만 자정까지 OUT 기록은 없는 경우
    for items, values in statusDict.items():
        if values:
            maxTime = 23*60 + 59
            resultDict[items] += maxTime - timeDict[items]

    # 주차 요금 환산
    nTime, nFee, perTime, perFee = map(int, fees)
    for i,v in sorted(resultDict.items()):
        totalPrice = 0
        # 기본 시간 초과
        if v > nTime:
            v -= nTime
            totalPrice += nFee
            totalPrice += math.ceil(v/perTime)*perFee
        else:
            totalPrice += nFee

        answer.append(totalPrice)

    return answer
  • 꼼꼼하게 조건을 보며 구현하면 무난한 문제이다.
  • 시간 적게 쓰고 푸는 것을 연습하자.

프로그래머스 연습문제: https://school.programmers.co.kr/learn/challenges

profile
CS, 개발 공부기록 🌱

0개의 댓글