Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags more
Archives
Today
Total
관리 메뉴

David의 블로그

프로그래머스 - 전국 대회 선발 본문

프로그래밍/코딩

프로그래머스 - 전국 대회 선발

David 리 2024. 7. 1. 19:41

문제설명>

0번부터 n - 1번까지 n명의 학생 중 3명을 선발하는 전국 대회 선발 고사를 보았습니다. 등수가 높은 3명을 선발해야 하지만, 
개인 사정으로 전국 대회에 참여하지 못하는 학생들이 있어 참여가 가능한 학생 중 등수가 높은 3명을 선발하기로 했습니다.
각 학생들의 선발 고사 등수를 담은 정수 배열 rank와 전국 대회 참여 가능 여부가 담긴 boolean 배열 attendance가 매개변수로 주어집니다. 
전국 대회에 선발된 학생 번호들을 등수가 높은 순서대로 각각 a, b, c번이라고 할 때 10000 × a + 100 × b + c를 return 하는 solution 함수를 작성해 주세요.

 

내 생각>

attndance boolean[]을 순회하여 true인 rank의 원소값들을 구한다.

key값을 정렬할 수 있는 TreeMap<>으로 "등수 = 인덱스" 값을 넣는다.

등수로 정렬된 값을 List<>에 담아 0,1,2번째 인덱스 값을 추출했다.

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
// 전국 대회 선발 고사
//        [3, 7, 2, 5, 4, 6, 1]    [false, true, true, true, true, false, false]    20403
//        [1, 2, 3]                [true, true, true]                                102
//        [6, 1, 5, 2, 3, 4]        [true, false, true, false, false, true]            50200
        int[] rank = {615234};
        boolean[] attendance = {truefalsetruefalsefalsetrue};
        int answer = 0;
        
        List<Integer> idxList = new ArrayList<>();
        // 등수, 인덱스
        TreeMap<Integer, Integer> treeMap = new TreeMap<Integer, Integer>();
        for (int i = 0; i < rank.length; i++) {
            if (attendance[i]) {
                treeMap.put(rank[i], i);
            }
        }
        System.out.println(treeMap);
        
        Collection<Integer> values = treeMap.values();
        for (Integer idx : values) {
            System.out.println(idx);
            idxList.add(idx);
        }
        
        answer = 10000 * idxList.get(0+ 100 * idxList.get(1+ idxList.get(2);
        System.out.println(answer);
cs