# ★★☆☆☆ 주차요금계산

# 구현코드

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int[] solution(int[] fees, String[] records) {
        int[] answer;
        Map<String, String> inTime = new HashMap<>();
        Map<String, Integer> accumulationTime = new HashMap<>();

        for(String record : records){
            String[] recordSplit = record.split(" ");
            String time = recordSplit[0];
            String carNumber = recordSplit[1];

            if(recordSplit[2].equals("IN")){
                inTime.put(carNumber,time);
            }
            else{
                String parkingInTime = inTime.get(carNumber);

                int pastTime = 0;
                if(accumulationTime.containsKey(carNumber)){
                    pastTime += accumulationTime.get(carNumber);
                }
                accumulationTime.put(carNumber,calcParkingTime(parkingInTime, time) + pastTime);
                inTime.remove(carNumber);
            }
        }

        for(String carNumber : inTime.keySet()){
            int pastTime = 0;
            String parkingInTime = inTime.get(carNumber);
            if(accumulationTime.containsKey(carNumber)){
                pastTime += accumulationTime.get(carNumber);
            }
            accumulationTime.put(carNumber,calcParkingTime(parkingInTime, "23:59") + pastTime);
        }

        String[] carNumbers = accumulationTime.keySet().toArray(new String[0]);
        Arrays.sort(carNumbers);
        answer = new int[carNumbers.length];
        for(int i = 0; i < answer.length; i++){
            answer[i] = settlementFee(accumulationTime.get(carNumbers[i]),fees);
        }

        return answer;
    }

    public int settlementFee(int accumulationTime, int[] fees){
        if(accumulationTime <= fees[0])
            return fees[1];
        accumulationTime -= fees[0];
        return fees[1]+(((accumulationTime % fees[2] == 0 ? 0 : 1) + (accumulationTime / fees[2]))*fees[3]);
    }

    public int calcParkingTime(String parkingInTime, String parkingOutTime){
        String[] splitInTime = parkingInTime.split(":");
        String[] splitOutTime = parkingOutTime.split(":");
        return (Integer.parseInt(splitOutTime[0])*60 + Integer.parseInt(splitOutTime[1])) - (Integer.parseInt(splitInTime[0])*60 + Integer.parseInt(splitInTime[1]));
    }

}
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
61
62
63