-
프로그래머스_주차 요금 계산(python)Algorithm/프로그래머스_생각정리 2022. 12. 8. 19:35
import math def solution(fees, records): answer = [] time = dict() inout = dict() money = dict() for i in records: if i[6:10] not in time: time[i[6:10]] = int(i[0:2]) * 60 + int(i[3:5]) inout[i[6:10]] = str(i[0:5]) + "in" else: if "IN" == i[11:13]: time[i[6:10]] += int(i[0:2]) * 60 + int(i[3:5]) inout[i[6:10]] = str(i[0:5]) + "in" else: time[i[6:10]] = int(i[0:2]) * 60 + int(i[3:5]) - time[i[6:10]] inout[i[6:10]] = "out" end_time = 23 * 60 + 59 for i in inout.keys(): if "in" in inout[i]: time[i] -= (int(inout[i][0:2])*60 + int(inout[i][3:5])) time[i] += end_time - (int(inout[i][0:2])*60 + int(inout[i][3:5])) for i in time.items(): if fees[0] >= i[1]: money[int(i[0])] = fees[1] else: money[int(i[0])] = fees[1] + math.ceil((i[1]-fees[0]) / fees[2]) * fees[3] print(money) for i in sorted(money.items(), key=lambda item:item[0]): answer.append(i[1]) return answer
처음에 이렇게 풀었다가 예제코드는 다 맞는데 채점을 하면 3,4,7,8,10번이 틀리길래 도대체 왜 틀리나 했는데 한번 입차하고 출차를 한 경우에만 통과가 되고 한번 더 입차를 하고 출차를 하게 되는 경우가 고려되지 않았다.
import math def solution(fees, records): answer = [] time = dict() inout = dict() money = dict() for i in records: if i[6:10] not in time: time[i[6:10]] = 0 inout[i[6:10]] =i[0:5] + "in" else: if "IN" == i[11:13]: inout[i[6:10]] = i[0:5] + "in" else: time[i[6:10]] += int(i[0:2]) * 60 + int(i[3:5]) - (int(inout[i[6:10]][0:2]) * 60 + int(inout[i[6:10]][3:5])) print(int(i[0:2]) * 60 + int(i[3:5]) , (int(inout[i[6:10]][0:2]) * 60 + int(inout[i[6:10]][3:5]))) inout[i[6:10]] = "out" end_time = 23 * 60 + 59 for i in inout.keys(): if "in" in inout[i]: time[i] += end_time - (int(inout[i][0:2])*60 + int(inout[i][3:5])) for i in time.items(): if fees[0] >= i[1]: money[int(i[0])] = fees[1] else: money[int(i[0])] = fees[1] + math.ceil((i[1]-fees[0]) / fees[2]) * fees[3] print(time) print(money) for i in sorted(money.items(), key=lambda item:item[0]): answer.append(i[1]) return answer
time 사전형에다가 시간을 누적은 해주되 처음 올렸던 코드와 달리 주차한 시간만 누적해주는 코드로 바꿔주었다.
'Algorithm > 프로그래머스_생각정리' 카테고리의 다른 글
프로그래머스_광물캐기_level2(python) (0) 2023.03.26 프로그래머스_두 큐 합 같게 만들기_level2(python) (0) 2023.03.23 프로그래머스_level 2_리코쳇 로봇(python) (0) 2023.03.19 프로그래머스 우박수열_정적분(python) (0) 2022.12.06 프로그래머스 택배상자(python) (0) 2022.10.18