Post

[프로그래머스/LV2] 2022 KAKAO BLIND RECRUITMENT - 주차 요금 계산 - HashMap (Java)

주차 요금 계산 문제를 Java로 해결한 풀이입니다. HashMap 알고리즘을 활용하여 규칙을 나누어 문제를 정의합니다.

2022 KAKAO BLIND RECRUITMENT - 주차 요금 계산

✅ 정답 코드

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.Map;


class Solution {
    public int[] solution(int[] fees, String[] records) {
        
        // 00시부터 23:59까지라 엣지케이스 필요없다
        // ex) 17:00 -> 04:53
        
        // 1. hash_map
        Map<String, int[]> hash_map = new HashMap<>();
        
        for (int i = 0; i < records.length; i++) {
            String pos = records[i];
            StringTokenizer st = new StringTokenizer(pos);
            
            String time = st.nextToken();
            String car_no = st.nextToken();
            String in_or_out = st.nextToken();
            String[] arr = time.split(":");
            int h = Integer.parseInt(arr[0]);
            int m = Integer.parseInt(arr[1]);
            
            // 1-1. IN일때
            if (in_or_out.equals("IN")) {
                // 1-1-a. hash_map에 차번호 없다면?
                if (!hash_map.containsKey(car_no)) {
                    int cur_sum = 0;
                    int cur_car_in = (h * 60) + m;
                    hash_map.put(car_no, new int[] {cur_sum, cur_car_in});
                }
                // 1-1-b. hashmap에 차번호 있다면?
                else if (hash_map.containsKey(car_no)) {
                    int[] p = hash_map.get(car_no);
                    int cur_sum = p[0];
                    int cur_car_in = (h * 60) + m;
                    hash_map.put(car_no, new int[] {cur_sum, cur_car_in});
                }
            }
            // 1-2. OUT일때
            else if (in_or_out.equals("OUT")) {
                int[] p = hash_map.get(car_no);
                int cur_sum = p[0];
                int prev_car_in = p[1];
                int cur_car_out = (h * 60) + m;
                int diff = cur_car_out - prev_car_in;
                
                cur_sum += diff;
                cur_car_out = -1;
                hash_map.put(car_no, new int[] {cur_sum, cur_car_out});
            }
        }
        
        // 3. 정산처리
        int size = hash_map.size();
        int[] res = new int[10000];
        boolean[] visited = new boolean[10000];
        
        for (String car_no : hash_map.keySet()) {
            int[] arr = hash_map.get(car_no);
            int cum_m = arr[0];
            int car_in = arr[1];
            
            // 3-1. 아직 출차안했으면?
            if (car_in != -1) {
                int diff = (60 * 23) + 59 - car_in;
                car_in = -1;
                cum_m += diff;
                hash_map.put(car_no, new int[] {cum_m, car_in});
            }
            
            // 3-2. 비용 계산
            int cost = 0;
            cost += fees[1];
            int extra = 0;
            if (cum_m > fees[0]) {
                int diff = cum_m - fees[0];
                extra = (int) Math.ceil( (double) diff / fees[2] ) * fees[3];
            }
            cost += extra;
            int idx = Integer.valueOf(car_no);
            visited[idx] = true;
            res[idx] = cost;
            
        }
        
        // 4. 정답
        int[] answer = new int[size];
        int final_idx = 0;
        for (int i = 0; i < 10000; i++) {
            if (visited[i]) {
                answer[final_idx] = res[i];
                final_idx++;
            }
        }
        
        
        
        return answer;
    }
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags

반갑습니다 무엇을 도와드릴까요?