CS/알고리즘_KAKAO BLIND RECRUITMENT

2021 KAKAO BLIND RECRUITMENT : 순위 검색(비트마스크 & 집합)

Jedy_Kim 2021. 10. 25. 19:50
728x90

https://programmers.co.kr/learn/courses/30/lessons/72412

 

코딩테스트 연습 - 순위 검색

["java backend junior pizza 150","python frontend senior chicken 210","python frontend senior chicken 150","cpp backend senior pizza 260","java backend junior chicken 80","python backend senior chicken 50"] ["java and backend and junior and pizza 100","pyt

programmers.co.kr

 

// 접근법

 

// 코드

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
import java.util.*;
 
class Solution {
    
    Map<String, Integer> Wordmap   = new HashMap<>();
    List<List<Integer>> Scorelist = new ArrayList<>();
    
    public int[] solution(String[] info, String[] query) {
        
        Wordmap.put("-"0);
        Wordmap.put("cpp"1);
        Wordmap.put("java"2);
        Wordmap.put("python"3);
        Wordmap.put("backend"1);
        Wordmap.put("frontend"2);
        Wordmap.put("junior"1);
        Wordmap.put("senior"2);
        Wordmap.put("chicken"1);
        Wordmap.put("pizza"2);
        
        for(int i=0; i<(4*3*3*3); ++i)
            Scorelist.add(new ArrayList<>());
        
        for(String str : info) {
            
            String[] word = str.split(" ");
            
            int[] arr = {
                Wordmap.get(word[0]) *3*3*3,
                Wordmap.get(word[1]) *3*3,
                Wordmap.get(word[2]) *3,
                Wordmap.get(word[3])
            };
            
            int score = Integer.parseInt(word[4]);
                                    
            for(int i=0; i<(1 << 4); ++i) {
                int idx = 0;
                for(int j=0; j<4++j) {
                    if( (i & (1<<j)) != 0)
                        idx += arr[j];
                }
                Scorelist.get(idx).add(score);
            }
        }
        
        // 이진 탐색을 하기 위해 정렬을 해준다.
        for(int i=0; i<4*3*3*3++i) 
            Collections.sort(Scorelist.get(i)); 
        
        int[] answer = new int[query.length];
        for(int i=0; i<query.length++i) {
            String[] word = query[i].split(" ");
            
            int idx = (Wordmap.get(word[0]) *3*3*3)
                + (Wordmap.get(word[2]) *3*3)
                + (Wordmap.get(word[4]) *3)
                + Wordmap.get(word[6]);
            
            int score = Integer.parseInt(word[7]);            
            int ret = Collections.binarySearch(Scorelist.get(idx), score);
 
            if(ret < 0) {
                ret = -(ret + 1);
            } else {
                // 같은 수일 경우 가장 낮은 개수를 찾기 위해
                // 예 - - - - 150 일 때 50 80 150 150 210 260 인경우
                // 만약 ret에는 3번째에 위치한 150을 가르키게되어 150 이상의 값을 구하면 3이된다.
                // 즉 이전의 150의 값은 누락되는 것이다. 그래서 이러한 누락을 방지하기 위해 아래와 같은 작업을 해준다.
                // 주어진 입출력에서는 사실상 문제가 없다.
                for(int j = ret - 1; j >= 0--j) {
                    if(Scorelist.get(idx).get(j) == score) ret = j;
                    else break;                    
                } 
                 
            }
            answer[i] = Scorelist.get(idx).size() - ret;
        }
        
        return answer;
    }
}
cs

 

// 비트 연산자 개념

https://www.youtube.com/watch?v=rwxqCSAiuS4&t=753s 

 

반응형