본 게시글은 2022 Kakao Blind Recruitment 코딩테스트 문제를 프로그래머스 코딩테스트연습에서 풀어본 내용을 기반으로 합니다.
해설, 정답코드만 보려면 목차 4. 문제 풀이로 넘어가주세요.
주차장의 요금표와 차량이 들어오고(입차) 나간(출차) 기록이 주어졌을 때, 차량별로 주차 요금을 계산하려고 합니다.
def solution(fees, records):
answer = []
#시간 계산
carstime = {}
finalprice = {}
for item in records :
eachcar = item.split(" ")
eachcar[0] = eachcar[0].split(":")
eachcar.append(int(eachcar[0][0]) * 60 + int(eachcar[0][1]))
if eachcar[1] not in carstime :
carstime[eachcar[1]] = eachcar[3]
else :
carstime[eachcar[1]] = eachcar[3] - carstime[eachcar[1]]
try : finalprice[eachcar[1]] += carstime.pop(eachcar[1]) # 현재 주차 목록에서 삭제
except : finalprice[eachcar[1]] = carstime.pop(eachcar[1])
for item in carstime :
try : finalprice[item] += (24*60 - 1) - carstime[item]
except : finalprice[item] = (24*60 - 1) - carstime[item]
for key in sorted(finalprice) :
tmptime = finalprice[key]
if tmptime > fees[0] :
tmpprice = fees[1] -((fees[0]-tmptime)//fees[2])*fees[3] #math import 없이 반올림
else :
tmpprice = fees[1]
answer.append(tmpprice)
return answer