[Programmers] 주차요금계산

Coodori·2023년 4월 14일
0

Programmers

목록 보기
6/10

주차 요금 계산

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

내 풀이(성공)

import math
def solution(fees, records):
    cars = []
    for i in records:
        time , num, d = i.split(" ")
        cars.append(num)
        
    cars = list(set(cars))
    cars.sort()
    answer = [0 for _ in range(len(cars))]
    
    for c in range(len(cars)):
        come = -1
        for i in records:
            time , num, d = i.split(" ")
            t, m = map(int, time.split(":"))
            if num  == cars[c] and come == -1:
                print(t, m)
                come = t * 60 + m
                continue
                
            if num == cars[c] and come !=  -1:
                answer[c] += t*60 +m - come
                come = -1
                
        if come != -1 :
            answer[c] += (23*60 + 59 - come)
            
    min_time, min_fee , plus_time, plus_fee = fees
    print(answer)
    for i in range(len(answer)):
        k = answer[i]
        if k - min_time <= 0:
            answer[i] = min_fee
        else:
            pf = math.ceil((k-min_time)/plus_time) * plus_fee
            answer[i] = pf + min_fee
            
    return answer

풀이 방법

구현, 문자열 파싱 문제였다. 풀이 방법 자체는 문제를 읽으면서 빠르게 생각났고 문제 자체에서 연산하는 방법도 명확하게 표현되어있어 그대로 구현하면 되는 문제였다.

math.ceil()을 사용해서 올림을 해서 초과한 요금을 계산하였다.

profile
https://coodori.notion.site/0b6587977c104158be520995523b7640

0개의 댓글