본문 바로가기

알고리즘/카카오

[프로그래머스] 양궁대회

반응형

https://school.programmers.co.kr/learn/courses/30/lessons/92342

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


import java.util.Arrays;

public class Solution {

	static int[] answer = {-1};
	static int[] lion = new int[11];
	static int max = -1;
	
	public static int[] solution(int n, int[] info) {
       
       	arrow(info, n);
       	
       	// System.out.println(Arrays.toString(answer));
       	
        return answer;
    }
	
	public static void arrow(int[] info, int depth) {
		if(depth <= 0) {
			
			int scoreApeach = 0;
			int scoreLion = 0;
			
			for(int i=0; i<info.length; i++) {
				if(info[i] == 0 && lion[i] == 0) {
					continue;
				} else if(info[i] >= lion[i]) {
					scoreApeach += 10-i;
				} else if(info[i] < lion[i]) {
					scoreLion += 10-i;
				}
			}
			
			if(scoreLion > scoreApeach) {
				if(scoreLion - scoreApeach == max) {
					boolean flag = false;
					
					for(int i=info.length-1; i>=0; i--) {
						if(answer[i] < lion[i]) {
							flag = true;
							break;
						}
					}
					
					if(flag) answer = lion.clone();
				} else if(scoreLion - scoreApeach > max) {
					answer = lion.clone();
					max = Math.max(max, scoreLion - scoreApeach);
				}
			}
			
			return;
		}
		
		
		for(int i=0; i<info.length && lion[i] <= info[i]; i++) {
			lion[i] += 1;
			arrow(info, depth-1);
			lion[i] -= 1;
		}
		
	}
}

 

반응형