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

 

프로그래머스

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

programmers.co.kr


내 풀이

 

from collections import defaultdict
import math

def solution(fees,records):
    info = {}
    answer=defaultdict(int)
    result =[]
    max_time = 23 * 60 + 59
    for i in records:
        time,number,status = i.split()
        hour,minu = time.split(':')
        time = int(hour) * 60 + int(minu)
        if number in info:
            answer[number] += time - info[number]
            
            del(info[number])
            
            
        else:
            info[number]= time
    max_time = 23 * 60 + 59
    for num,t in info.items():
        answer[num] += max_time - t
    
    
    basic_t,basic_f,m_t,m_f = fees
    for num,time in answer.items():
        cost = basic_f
        if basic_t < time:
            cost += math.ceil ((time - basic_t)  / m_t)  * m_f
        result.append((num,cost))
    result.sort()
    
    return [i[1] for i in result]

진짜 오래걸렸다. 이유는 모르겠음 한 두시간은 푼거같다. 어렵지도 않았는데 말이지

 


다른사람의 풀이

from collections import defaultdict
from math import ceil

class Parking:
    def __init__(self, fees):
        self.fees = fees
        self.in_flag = False
        self.in_time = 0
        self.total = 0

    def update(self, t, inout):
        self.in_flag = True if inout=='IN' else False
        if self.in_flag:  self.in_time = str2int(t)
        else:             self.total  += (str2int(t)-self.in_time)

    def calc_fee(self):
        if self.in_flag: self.update('23:59', 'out')
        add_t = self.total - self.fees[0]
        return self.fees[1] + ceil(add_t/self.fees[2]) * self.fees[3] if add_t >= 0 else self.fees[1]

def str2int(string):
    return int(string[:2])*60 + int(string[3:])

def solution(fees, records):
    recordsDict = defaultdict(lambda:Parking(fees))
    for rcd in records:
        t, car, inout = rcd.split()
        recordsDict[car].update(t, inout)
    return [v.calc_fee() for k, v in sorted(recordsDict.items())]

 



 

+ Recent posts