https://school.programmers.co.kr/learn/courses/30/lessons/92342
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
저는 dfs를 통해 문젤르 해결하였습니다.
라이언이 해당 depth(점수)에 몇개를 넣을 지를 결정하였고, 라이언이 가장 큰 점수 차이로 우승할 수 있는 방법이 여러개라면, 가장 낮은 점수를 많이 맞친 경우를 return 해야하기 때문에 저는 남은 화살은 전부 0에 넣도록 구현하였습니다.
그리고 결과값의 초기화는 같은 점수 차라면 배열의 뒤 (점수가 낮은 부분) 부터 개수를 비교하여 더 큰쪽으로 초기화 시켜 문제를 해결하였습니다.
import java.util.*;
class Solution {
private static final int[] NOT_FOUND = {-1};
private static final int SIZE = 11;
private static int[] answer = NOT_FOUND;
private static int answerGap = 0;
public int[] solution(int n, int[] info) {
int[] score = new int[SIZE];
dfs(0,n,info,score);
return answer;
}
private static void dfs(int depth, int n, int[] info, int[] score){
if(depth == SIZE){
int gap = 0;
if(n > 0){
score[SIZE-1] += n;
}
for(int i = 0 ; i < SIZE ; i++){
if(info[i] < score[i]){
gap += 10 - i;
}else if(info[i] != 0){
gap -= 10 - i;
}
}
if(gap == answerGap && gap != 0){
for(int i = SIZE - 1 ; i >= 0 ; i--){
if(answer[i] < score[i]){
answer = Arrays.copyOf(score,SIZE);
break;
}else if(answer[i] > score[i]){
break;
}
}
}
if(gap > answerGap){
answerGap = gap;
answer = Arrays.copyOf(score,SIZE);
}
if(n > 0){
score[SIZE-1] -= n;
}
return ;
}
if(n > info[depth]){
score[depth] = info[depth] + 1;
dfs(depth+1,n - info[depth] - 1,info,score);
score[depth] = 0;
}
dfs(depth+1,n,info,score);
return ;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 택배 배달과 수거하기 (JAVA) (0) | 2023.08.11 |
---|---|
프로그래머스 파괴되지 않은 건물 (JAVA) (0) | 2023.08.10 |
프로그래머스 신고 결과 받기 (JAVA) (0) | 2023.08.08 |
프로그래머스 주차 요금 계산 (JAVA) (0) | 2023.08.07 |
프로그래머스 k진수에서 소수 개수 구하기 (JAVA) (0) | 2023.08.06 |