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

KIMYEONGJUN·2026년 7월 28일
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

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

내가 이 문제를 보고 생각해본 부분

먼저 solution 메서드에서 학생 수 n을 구한다.
그다음 학생 번호와 등수를 쌍으로 묶어 List<int[]>에 저장한다. 이때 각 요소는 {학생번호, 등수} 형식이다.
리스트를 등수 기준 오름차순으로 정렬한다. 등수가 1이면 가장 높은 순위를 의미한다.
정렬된 리스트를 순서대로 탐색하면서 해당 학생이 대회에 참석 가능한지(attendance[idx] 값이 true인지`) 확인한다.
참석 가능한 학생마다 selected 리스트에 학생 번호를 추가한다. 3명이 되면 반복을 종료한다.
마침내 선발된 3명의 학생 번호를 a, b, c에 각각 담는다.
문제 요구대로 10000 × a + 100 × b + c를 계산해 그 값을 반환한다.

코드로 구현

import java.util.*;

class Solution {
    public int solution(int[] rank, boolean[] attendance) {
        int n = rank.length;

        List<int[]> students = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            students.add(new int[]{i, rank[i]});
        }

        students.sort(Comparator.comparingInt(a -> a[1]));
        List<Integer> selected = new ArrayList<>();

        for (int[] student : students) {
            int idx = student[0];
            if (attendance[idx]) {
                selected.add(idx);
                if (selected.size() == 3) break;
            }
        }

        int a = selected.get(0);
        int b = selected.get(1);
        int c = selected.get(2);
        
        return 10000 * a + 100 * b + c;
    }
}

프로그래머스 코드

package programmers;

import java.util.*;

// 프로그래머스 전국 대회 선발 고사
public class Main94 {
    public static void main(String[] args) {
        Main94 sol = new Main94();

        int[] rank1 = {3, 7, 2, 5, 4, 6, 1};
        boolean[] attendance1 = {false, true, true, true, true, false, false};
        System.out.println(sol.solution(rank1, attendance1));  // 출력 예상: 20403

        int[] rank2 = {1, 2, 3};
        boolean[] attendance2 = {true, true, true};
        System.out.println(sol.solution(rank2, attendance2));  // 출력 예상: 102

        int[] rank3 = {6, 1, 5, 2, 3, 4};
        boolean[] attendance3 = {true, false, true, false, false, true};
        System.out.println(sol.solution(rank3, attendance3));  // 출력 예상: 50200
    }

    public int solution(int[] rank, boolean[] attendance) {
        int n = rank.length;

        List<int[]> students = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            students.add(new int[]{i, rank[i]});
        }

        students.sort(Comparator.comparingInt(a -> a[1]));

        List<Integer> selected = new ArrayList<>();

        for (int[] student : students) {
            int idx = student[0];
            if (attendance[idx]) {
                selected.add(idx);
                if (selected.size() == 3) break;
            }
        }

        int a = selected.get(0);
        int b = selected.get(1);
        int c = selected.get(2);

        return 10000 * a + 100 * b + c;
    }
}

위에 있는 코드를 변경한 코드

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글