-
[프로그래머스] LV2. 주차요금 계산알고리즘 2023. 3. 20. 14:26728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
접근
1. 들어가고 나오는 순서가 중요하지 않으니 stack,queue 말고 dict으로 풀면 된다 생각함
2. status를 두어서 후에 같은 차량이 들어와도 상관없게 함
3. status을 기준으로 누적 시간을 구함
4. ceil 대신에 시간이 더 있으면 한번더 추가요금을 내는걸로 계산함
풀이
from collections import defaultdict def solution(fees, records): ans = [] res = [] dic = defaultdict(list) for i in records: data = i.split(' ') times = data[0].split(':') time = int(times[0]) * 60 + int(times[1]) if data[1] not in dic: dic[data[1]] = [time, data[2], True] else: if dic[data[1]][2] == True: dic[data[1]][2] = False res.append([time - dic[data[1]][0], data[1]]) else: dic[data[1]] = [time, data[2], True] for d in dic.items(): #print(d[1][2]) #print(d) if d[1][2] == True: res.append([1439-d[1][0] , d[0]]) #print(res) time = {} for i in res: if i[1] not in time: time[i[1]] = i[0] else: time[i[1]] += i[0] #print(time) basicTime,basicMoney,addTime,addMoney= fees sorted_time = dict(sorted(time.items(), key=lambda x: x[0])) #print(sorted_time) for t in sorted_time.items(): if t[1] <= basicTime: money = basicMoney else: tt = t[1] - basicTime if tt % addTime == 0: money = basicMoney + (tt // addTime) * addMoney else: money = basicMoney + (tt // addTime) * addMoney + addMoney ans.append(money) money = 0 return ans
시간은 좀 걸리고, 코드가 복잡하지만 스스로 풀어서 대견함
'알고리즘' 카테고리의 다른 글
[프로그래머스] [3차] n진수 게임 (0) 2023.03.28 [프로그래머스] LV2. 1차 캐시 (0) 2023.03.27 [프로그래머스] LV1. 모의고사 (0) 2023.03.13 [프로그래머스] LV1. 성격 유형 검사하기 (0) 2023.03.13 [프로그래머스] LV1. 둘만의 암호 (0) 2023.03.13