1월 24일(수) - 6장 완전 탐색
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.stream.IntStream;
public class Solution {
private static int[][] RULES={
{1,2,3,4,5},
{2,1,2,3,2,4,2,5},
{3,3,1,1,2,2,4,4,5,5},
};
private int get_picked(int person, int problem){
int[] rule = RULES[person];
int idx = problem % rule.length;
return rule[idx];
}
public int[] solution(int[] answers){
int[] corrects = new int[3];
int max = 0;
for(int problem=0; problem< answers.length; problem++)
{
int answer = answers[problem];
for(int person=0; person<3; person++)
{
int picked = get_picked(person, problem);
if(answer == picked)
{
if(++corrects[person] > max)
{
max = corrects[person];
}
}
}
}
final int max_corrects = max;
return IntStream.range(0,3)
.filter(i -> corrects[i] == max_corrects)
.map(i -> i+1)
.toArray();
}
}
댓글