티스토리 뷰

체육대회 문제 링크

정답 코드

import java.util.stream.IntStream;

class Solution {
    int[] board = new int[11];
    int result;
    public int solution(int[][] ability) {
        for(int i = 0; i < ability.length; i++) board[i] = i;
        permutation(0, ability);
        return result;
    }
    private void permutation(int depth, int[][] ability) {
        int r = ability[0].length;
        if(depth == r) {
            result = Math.max(result, IntStream.range(0, r).map((i) -> ability[board[i]][i]).sum());
            return;
        }
        for(int i = depth; i < ability.length; i++) {
            swap(i, depth);
            permutation(depth + 1, ability);
            swap(depth, i);
        }
    }
    private void swap(int a, int b) {
        int tmp = board[b];
        board[b] = board[a];
        board[a] = tmp;
    }
}

문제 해설

  1. 학생들의 인덱스 번호를 배열에 담는다.
  2. 순열을 이용하여 종목 개수만큼 학생을 뽑았을 경우 능력치의 합을 구한다.
  3. 가장 큰 합과 비교하여 큰 합을 저장한다.
  4. 2~3번을 반복한다.
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함