프로그래밍 공방

[프로그래머스] 순위 검색 본문

개발/문제해결

[프로그래머스] 순위 검색

hyosupsong 2021. 2. 4. 22:32

문제

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package Programmers;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
public class Solution_순위검색 {
 
    public static List<Integer>[][][][] list = new List[3][2][2][2];
 
    public static void init() {
        for(int i=0; i<3; i++) {
            for(int j=0; j<2; j++) {
                for(int k=0; k<2; k++) {
                    for(int z=0; z<2; z++) {
                        list[i][j][k][z] = new ArrayList<Integer>();
                    }
                }
            }
        }
    }
    
    public static void sort() {
        for(int i=0; i<3; i++) {
            for(int j=0; j<2; j++) {
                for(int k=0; k<2; k++) {
                    for(int z=0; z<2; z++) {
                        Collections.sort(list[i][j][k][z]);
                    }
                }
            }
        }
    }
 
    public static int stringToInt(String s) {
        int num = 0;
        if(s.equals("cpp"|| s.equals("backend"|| s.equals("junior"|| s.equals("chicken")) num = 0;
        else if(s.equals("java"|| s.equals("frontend"|| s.equals("senior"|| s.equals("pizza")) num = 1;
        else num = 2;
        return num;
    }
 
    public static int getOverScore(List<Integer> list, int score) {
        int s = 0, e = list.size()-1;
        int mid;
        while(s<=e) {
            mid = (s+e)>>1;
            if(list.get(mid)<score) s = mid+1;
            else e = mid-1;
        }
        return list.size() - s;
    }
 
    public static int[] solution(String[] info, String[] query) {
        List<Integer> countList = new ArrayList<>();
        init();
        String language, field, rank, food;
        for(int i=0; i<info.length; i++) {
            String[] s = info[i].split(" ");
            language = s[0];
            field = s[1];
            rank = s[2];
            food = s[3];
            int score = Integer.parseInt(s[4]);
            list[stringToInt(language)][stringToInt(field)][stringToInt(rank)][stringToInt(food)].add(score);
        }
        sort();
        int[] la = {012}, fi= {01}, ra= {01}, fo= {01};
        int lalength, filength, ralength, folength;
        for(int i=0; i<query.length; i++) {
            String[] s = query[i].split(" ");
            language = s[0];
            field = s[2];
            rank = s[4];
            food = s[6];
            int score = Integer.parseInt(s[7]);
            if(language.equals("-")) {
                la[0= 0;
                lalength = 3;
            } else {
                la[0= stringToInt(language);
                lalength = 1;
            }
            if(field.equals("-")) {
                fi[0= 0;
                filength = 2;
            } else {
                fi[0= stringToInt(field);
                filength = 1;
            }
            if(rank.equals("-")) {
                ra[0= 0;
                ralength = 2;
            } else {
                ra[0= stringToInt(rank);
                ralength = 1;
            }
            if(food.equals("-")) {
                fo[0= 0;
                folength = 2;
            } else {
                fo[0= stringToInt(food);
                folength = 1;
            }
            int sum = 0;
            for(int l=0; l<lalength; l++) {
                for(int j=0; j<filength; j++) {
                    for(int k=0; k<ralength; k++) {
                        for(int z=0; z<folength; z++) {
                            sum += getOverScore(list[la[l]][fi[j]][ra[k]][fo[z]], score);
                        }
                    }
                }
            }
            countList.add(sum);
        }
        int[] answer = new int[countList.size()];
        for(int i=0; i<countList.size(); i++) answer[i] = countList.get(i);
        return answer;
    }
    
    public static void main(String[] args) {
        String[] info = {"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"};
        String[] query = {"java and backend and junior and pizza 100","python and frontend and senior and chicken 200","cpp and - and senior and pizza 250","- and backend and senior and - 150","- and - and - and chicken 100","- and - and - and - 150"};
        System.out.println(Arrays.toString(solution(info, query)));
    }
}
cs


코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.

'개발 > 문제해결' 카테고리의 다른 글

[프로그래머스] 메뉴 리뉴얼  (0) 2021.02.08
[백준] 5676번 : 음주 코딩  (0) 2021.02.05
[백준] 14425번 : 문자열 집합  (0) 2021.02.02
[프로그래머스] 외벽 점검  (0) 2021.01.29
[프로그래머스] 광고 삽입  (5) 2021.01.27