-
Notifications
You must be signed in to change notification settings - Fork 0
/
주차요금계산.js
60 lines (46 loc) · 1.63 KB
/
주차요금계산.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Solution 1
function getMinutes(time) {
const splittedTime = time.split(':');
const hours = parseInt(splittedTime[0]);
const minute = parseInt(splittedTime[1]);
return hours * 60 + minute;
}
function solution(fees, records) {
const result = [];
const [basicMinutes, basicFee, unitTime, unitFee] = fees;
const totalTimeMap = new Map();
const _records = records
.map((record) => record.split(' '))
.sort((a, b) => parseInt(a[1]) - parseInt(b[1]));
function setTotalTimeMap(carNumber, minutes) {
totalTimeMap.has(carNumber)
? totalTimeMap.set(carNumber, totalTimeMap.get(carNumber) + minutes)
: totalTimeMap.set(carNumber, minutes);
}
for (let i = 0; i < _records.length; i += 2) {
const [firstTime, firstCarNumber, firstAction] = _records[i];
if (i === _records.length - 1) {
const minutes = getMinutes('23:59') - getMinutes(firstTime);
setTotalTimeMap(firstCarNumber, minutes);
break;
}
const [secondTime, secondCarNumber, secondAction] = _records[i + 1];
if (firstCarNumber === secondCarNumber) {
const minutes = getMinutes(secondTime) - getMinutes(firstTime);
setTotalTimeMap(firstCarNumber, minutes);
} else if (firstCarNumber !== secondCarNumber) {
const minutes = getMinutes('23:59') - getMinutes(firstTime);
setTotalTimeMap(firstCarNumber, minutes);
i--;
}
}
for (const [carNumber, totalMinutes] of totalTimeMap) {
const fee =
totalMinutes <= basicMinutes
? basicFee
: basicFee +
Math.ceil((totalMinutes - basicMinutes) / unitTime) * unitFee;
result.push(fee);
}
return result;
}