내 인생은 개발 중
[프로그래머스] 주차 요금 계산 - Python 본문
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
def solution(fees, records):
car_time = {}
answer = []
for car in records:
time, number, status = car.split()
# 해당 차량이 딕셔너리에 없으면 추가, 입차 시간 등록하기
if not number in car_time:
car_time[number] = [False, (int(time[:2]) * 60) + (int(time[3:]))]
# 차량이 딕셔너리에 있으면
else:
# 출차인 경우에는 상태를 True로 바꿔주고 주차 시간 구하기
if status == 'OUT':
car_time[number][0] = True
car_time[number][-1] = (int(time[:2]) * 60) + (int(time[3:])) - car_time[number][-1]
# 입차인 경우에는 상태를 False로 바꿔주고 입차 시간 분으로 환산해서 추가하기
else:
car_time[number][0] = False
car_time[number].append((int(time[:2]) * 60) + (int(time[3:])))
# 차량번호 기준으로 오름차순 정렬
sorted_car_time = sorted(car_time.items())
for records in sorted_car_time:
fee = 0
total_time = 0
# 상태가 True 즉, 정상적으로 출차가 된 경우 누적 주차 시간 구해서 기본 주차 시간 빼기
if records[1][0] == True:
for record in range(1, len(records[1])):
total_time += records[1][record]
total_time -= fees[0]
# 출차가 안된 경우
else:
# 주차장에 2번 이상 방문한 경우 정상적으로 출차 된 주차 시간 더하기
if len(records[1]) >= 3:
for record in range(1, len(records[1]) - 1):
total_time += records[1][record]
# 출차가 안됐으면 11:59에 출차된 것으로 간주하고 주차시간 계산해서 더하고 기본 주차 시간 빼기
total_time += 1439 - records[1][-1] - fees[0]
# 누적 주차 시간 > 기본 주차 시간 이면
if total_time > 0:
# 초과한 시간이 단위 시간으로 나누어 떨어지지 않으면 올림해서 계산해주기
if total_time % fees[2] > 0:
fee = fees[1] + ((total_time // fees[2]) + 1) * fees[3]
else:
fee = fees[1] + (total_time // fees[2]) * fees[3]
# 누적 주차 시간 < 기본 주차 시간 이면 기본 요금만 계산
else:
fee = fees[1]
answer.append(fee)
return answer
💡알게 된 점💡
딕셔너리 value로 여러 값을 두고싶으면 list를 이용하면 된다.
딕셔너리를 키 기준으로 정렬하려면 sorted_dict = sorted(dict.items())
이렇게 사용하면 된다.sorted(dict)
이렇게 사용한다면 키만 뽑아서 정렬하니 주의해야 한다!
푸는데 정말 많은 시간이 걸린 문제였다. 그래도 2022년도 문제긴 하지만 카카오 문제를 검색 안하고 혼자 푼것에 만족한다! 히히
근데 좀 그리디하게 푼 것 같아서 좀 더 효율적으로 풀 수 있게 나중에 다시 풀어봐야겠다!
'Algorithm > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 괄호 회전하기 - Python (0) | 2024.06.05 |
---|---|
[프로그래머스] 연속 부분 수열 합의 개수 - Python (0) | 2024.05.30 |
[프로그래머스] 짝지어 제거하기 - Python (0) | 2024.05.20 |
[프로그래머스] 롤케이크 자르기 - Python (0) | 2024.05.19 |
[프로그래머스] 피보나치 수 - Python (0) | 2024.05.18 |
Comments