<주차 요금 계산>
문제(출처: https://school.programmers.co.kr/learn/courses/30/lessons/92341)
문제 풀이
- my solution
def solution(fees, records):
answer = []
dict={} # 차 번호: 누적 주차 시간
temp={} # 차 번호: 입차 시간
for i in records:
x=i.split()
if x[2]=="IN":
if x[1] not in dict:
dict[x[1]]=0
temp[x[1]]=x[0] # 입차 시간
else: #시간 계산
outtime=list(map(int,x[0].split(":")))
intime=list(map(int, temp[x[1]].split(":")))
del temp[x[1]]
time=(outtime[0]-intime[0])*60+(outtime[1]-intime[1])
dict[x[1]]+=time
for key,value in temp.items(): #23:59분 출차
intime=list(map(int, value.split(":")))
outtime=[23,59]
time=(outtime[0]-intime[0])*60+(outtime[1]-intime[1])
dict[key]+=time
for key,value in dict.items():
result=fees[1] #기본 요금
if value-fees[0]>0: #기본 요금 초과시
if (value-fees[0])%fees[2]!=0:
result+=(((value-fees[0])//fees[2])+1)*fees[3]
else:
result+=((value-fees[0])//fees[2])*fees[3]
dict[key]=result # 차 번호: 주차 요금
dict=sorted(dict.items()) #차량 번호가 작은 자동차부터
for i in dict: #list
answer.append(i[1])
return answer
1) IN일 때 차 번호에 해당하는 key 값이 없으면 key, value 생성/ 입차 시간을 따로 저장
2) OUT일 때 IN 시간 계산하여 dict에 차 번호: 시간으로 넣어줌
3) IN만 있고 OUT이 없는 것 처리
4) 요금 계산
5) 차량 번호가 작은 자동차부터 처리하기 위해 정렬
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
'🌞Algorithm > 🔥programmers' 카테고리의 다른 글
| [programmers] JOIN문 - SQL 고득점 Kit (1) | 2022.02.06 |
|---|---|
| [programmers] 가장 먼 노드 (0) | 2022.02.03 |
| [programmers] 신고 결과 받기 - 2022 KAKAO BLIND RECRUITMENT (0) | 2022.01.17 |
| [programmers] GROUP BY문 - SQL 고득점 Kit (0) | 2022.01.17 |
| [programmers] IS NULL문 - SQL 고득점 Kit (0) | 2022.01.15 |