본문 바로가기
프로그래머스/2단계

(Python/LV2)주차 요금 계산

by windy7271 2022. 9. 27.
728x90
반응형

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

 

프로그래머스

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

programmers.co.kr

풀이:

import math


def get_fee(time, fees):
    return fees[1] + math.ceil(max(0, (time - fees[0])) / fees[2]) * fees[3]

def solution(fees, records):
    parking = {}
    total = {}
    for record in records:
        time, car, status = record.split()
        hour, minute = time.split(":")
        minutes = int(hour) * 60 + int(minute)
        if status == 'IN': # 들어간 시간
            parking[car] = minutes
        # elif status == 'OUT': # 나온 시간 출차 - 입차
        #     if total.get(car):
        #         total[car] += minutes - total[car]
        #     else:
        #         total[car] = minutes - total[car]
        elif status == 'OUT':
            try:
                total[car] += minutes - parking[car]
            except:
                total[car] = minutes - parking[car]
            del parking[car] # 출차 차량 삭제

    for car, minute in parking.items():
        try:
            total[car] += 23 * 60 + 59 - minute
        except:
            total[car] = 23 * 60 + 59 - minute
    return [get_fee(time, fees) for car, time in sorted(list(total.items()), key=lambda x: x[0])] # 차량 번호순으로 정렬

# 친 부분이 되지 않는다 너무 화난다 왜 안되는지 도저히 모르겠다.

 

다른분의 풀이를 활용했다.

반응형

댓글