honey_pot

[python] 주차 요금 계산 본문

문제 풀이

[python] 주차 요금 계산

_tera_ 2022. 7. 31. 22:45

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


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import math
from collections import defaultdict
 
def solution(fees, records):
    table = defaultdict(list)
    for idx in range(len(records)):
        time, number, code = records[idx].split()
        minutes = int(time[:2]) * 60 + int(time[3:]) # 시간을 모두 분으로 바꾸어 저장
        table[number].append(minutes) # 입차-출차 순서로 저장되기 때문에 코드는 딱히 의미 없으므로 시간만 저장
 
    # 정보의 개수가 홀수인 경우 마지막에 23:59 출차 정보를 넣어준다
    for i in table:
        if len(table[i]) % 2 == 1:
            table[i].append(23*60+59)
 
    # 차 번호 오름차순으로 정렬하여 그 번호에 해당하는 값을 테이불에서 찾는다.
    cars = sorted(table.keys())
    answer = [] 
 
    for car in cars:
        money = 0
        elapsed = sum(table[car][1::2]) - sum(table[car][::2]) # 총 이용 시간 = 짝수 인덱스 합(출차 시간) - 홀수 인덱스 합(입차 시간)
        if elapsed > fees[0]: # 이용 시간이 기본 시간 초과인 경우
            money += fees[1+ math.ceil((elapsed - fees[0]) / fees[2]) * fees[3]
        else# 이용 시간이 기본 시간 내인 경우
            money += fees[1]
        answer.append(money)
    return answer
cs

'문제 풀이' 카테고리의 다른 글

[python] 방금 그곡  (0) 2022.08.05
[Python] [3차] 파일명 정렬  (0) 2022.08.03
[Python] 올바른 괄호  (0) 2022.07.14
[Python] 땅따먹기  (0) 2022.07.14
[Python] 다음 큰 숫자  (0) 2022.07.12
Comments