|
11 | 11 | **/ |
12 | 12 |
|
13 | 13 | function toMin(time) { |
14 | | - let [h, m] = time.split(":").map(Number); |
15 | | - return h * 60 + m; |
| 14 | + const [hour, min] = time.split(":").map(Number); |
| 15 | + return hour * 60 + min; |
16 | 16 | } |
17 | 17 |
|
18 | 18 | function solution(fees, records) { |
19 | | - let inTime = {}; //{차 번호: 입차시간(분)} |
20 | | - let totalT = {}; //{차 번호: 누적분} |
21 | | - let answer = []; |
| 19 | + const car = {}; // {id: 입차시각(분)} |
| 20 | + const ing_time = {}; |
| 21 | + const result = []; |
22 | 22 |
|
23 | 23 | for (let record of records) { |
24 | | - let [time, car, type] = record.split(" "); |
| 24 | + const [time, id, command] = record.split(" "); |
25 | 25 |
|
26 | | - if (type === "IN") { |
27 | | - inTime[car] = toMin(time); |
| 26 | + if (command === "IN") { |
| 27 | + car[id] = toMin(time); |
28 | 28 | } else { |
29 | | - totalT[car] = (totalT[car] || 0) + (toMin(time) - inTime[car]); |
30 | | - delete inTime[car]; |
| 29 | + ing_time[id] = (ing_time[id] || 0) + toMin(time) - car[id]; |
| 30 | + delete car[id]; |
31 | 31 | } |
32 | 32 | } |
33 | 33 |
|
34 | | - for (let car of Object.keys(inTime)) { |
35 | | - totalT[car] = (totalT[car] || 0) + (toMin("23:59") - inTime[car]); |
| 34 | + for (let c of Object.keys(car)) { |
| 35 | + //car에 남아있는건 OUT안된거 |
| 36 | + ing_time[c] = (ing_time[c] || 0) + toMin("23:59") - car[c]; |
36 | 37 | } |
37 | 38 |
|
38 | | - for (let car of Object.keys(totalT).sort()) { |
39 | | - if (fees[0] < totalT[car]) { |
40 | | - answer.push( |
41 | | - fees[1] + Math.ceil((totalT[car] - fees[0]) / fees[2]) * fees[3] |
| 39 | + for (let c of Object.keys(ing_time).sort()) { |
| 40 | + if (fees[0] < ing_time[c]) { |
| 41 | + result.push( |
| 42 | + fees[1] + Math.ceil((ing_time[c] - fees[0]) / fees[2]) * fees[3] |
42 | 43 | ); |
43 | 44 | } else { |
44 | | - answer.push(fees[1]); |
| 45 | + result.push(fees[1]); |
45 | 46 | } |
46 | 47 | } |
47 | | - return answer; |
| 48 | + return result; |
48 | 49 | } |
0 commit comments