본문 바로가기

알고리즘/프로그래머스

[Python] 주차 요금 계산

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

 

프로그래머스

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

programmers.co.kr

# 주차 요금 계산

import math

def solution(fees, records):
    answer = []
    car_history = {}
    
    car_list = [i.split() for i in records]
    car_list.sort(key= lambda x : x[1])
    
    # 차 번호와 출입 시간이 기록된 딕셔너리
    for car in car_list:
        if car[1] not in car_history:
            car_history[car[1]] = [car[0].split(':')]
        else:
            car_history[car[1]].append(car[0].split(':'))
    
    # 각 차량이 총 주차한 시간을 분으로 표시
    for car, time in car_history.items():
        total_time = []
        total_h = 0
        total_m = 0
        
        if len(time) % 2 != 0:
            time.append(['23','59'])
            
        for i in range(len(time)-1):
            if i % 2 == 0:
                h = (int(time[i+1][0]) - int(time[i][0]))
                m = (int(time[i+1][1]) - int(time[i][1]))
                if m < 0:
                    h -= 1
                    m += 60
                    total_h += h
                    total_m += m
                else:
                    total_h += h
                    total_m += m
        
        total_h *= 60
        
        fee = 0
        total_time = total_h + total_m
        if total_time < fees[0]:
            fee = fees[1]
            answer.append(fee)
            
        elif total_time >= fees[0]:
            fee += fees[1]
            extra_time = total_time - fees[0]
            extra_fee = math.ceil(extra_time / fees[2])*fees[3]
            fee += extra_fee
            answer.append(fee)
        
    return answer

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[Python] n진수 게임  (0) 2023.01.24
[Python] 연속 부분 수열 합의 개수  (0) 2023.01.20
[Python] 가장 가까운 같은 글자  (0) 2023.01.16
[Python] 오픈 채팅방  (1) 2023.01.13
[Python] 피로도  (0) 2023.01.12