티스토리 뷰
체육대회 문제 링크
정답 코드
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;
}
}
문제 해설
- 학생들의 인덱스 번호를 배열에 담는다.
- 순열을 이용하여 종목 개수만큼 학생을 뽑았을 경우 능력치의 합을 구한다.
- 가장 큰 합과 비교하여 큰 합을 저장한다.
- 2~3번을 반복한다.
'알고리즘 > Programmers' 카테고리의 다른 글
[PCCP 모의고사 1] 3번 유전 법칙 [JAVA] (0) | 2023.04.25 |
---|---|
[PCCP 모의고사 1] 1번 외톨이 알파벳 [JAVA] (0) | 2023.04.23 |
[Level2] 광물 캐기 (JAVA) (0) | 2023.04.22 |
[Level2] 무인도 여행 (JAVA) (0) | 2023.04.20 |
[Level2] 과제 진행하기 (JAVA) (0) | 2023.04.19 |
댓글