Skip to content

Commit aad84f1

Browse files
committed
[level 2] Title: 주차 요금 계산, Time: 6.20 ms, Memory: 34.5 MB -BaekjoonHub
1 parent 4f297bb commit aad84f1

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

프로그래머스/2/92341. 주차 요금 계산/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 34.8 MB, 시간: 6.59 ms
7+
메모리: 34.5 MB, 시간: 6.20 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 09월 09일 23:10:10
19+
2025년 09월 09일 23:11:54
2020

2121
### 문제 설명
2222

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
function solution(fees, records) {
2-
const parkingTime = {};
3-
records.forEach(r => {
4-
let [time, id, type] = r.split(' ');
5-
let [h, m] = time.split(':');
6-
time = (h * 1) * 60 + (m * 1);
7-
if (!parkingTime[id]) parkingTime[id] = 0;
8-
if (type === 'IN') parkingTime[id] += (1439 - time);
9-
if (type === 'OUT') parkingTime[id] -= (1439 - time);
10-
});
11-
const answer = [];
12-
for (let [car, time] of Object.entries(parkingTime)) {
13-
if (time <= fees[0]) time = fees[1];
14-
else time = Math.ceil((time - fees[0]) / fees[2]) * fees[3] + fees[1]
15-
answer.push([car, time]);
2+
// 정렬 차량번호가 작은순
3+
records = records.map(r => r.split(" "));
4+
records.sort((a, b) => a[1] - b[1]); // 오름차순
5+
6+
// {차량번호: [시각, 시각, 시각, ...]}
7+
const car_map = new Map();
8+
9+
for (const r of records) {
10+
const times = car_map.get(r[1]) || [];
11+
times.push(r[0])
12+
car_map.set(r[1], times)
1613
}
17-
return answer.sort((a, b) => a[0] - b[0]).map(v => v[1]);
14+
15+
// 누적 시간 구하기
16+
const total_times = [];
17+
// 문자열 변환
18+
const str_to_time = (str_time) => {
19+
const [hour, min] = str_time.split(":");
20+
return Number(hour) * 60 + Number(min);
21+
}
22+
23+
car_map.forEach(car_in_out => {
24+
// 마지막 출차가 없는 경우 23:59를 마지막으로 간주
25+
if (car_in_out.length % 2 !== 0) car_in_out.push('23:59');
26+
27+
let total_time = 0;
28+
for (let i = 0; i < car_in_out.length; i += 2) {
29+
const start = str_to_time(car_in_out[i]);
30+
const end = str_to_time(car_in_out[i + 1]);
31+
const timeDiff = end - start;
32+
total_time += timeDiff;
33+
}
34+
total_times.push(total_time);
35+
})
36+
37+
// 요금
38+
const calc_fee = (time) => {
39+
const extra_charge = time < fees[0] ? 0 : Math.ceil((time - fees[0]) / fees[2]) * fees[3];
40+
return fees[1] + extra_charge;
41+
}
42+
43+
return total_times.map(time => calc_fee(time));
1844
}

0 commit comments

Comments
 (0)