P.192
자바 코드
import java.util.stream.IntStream;
public class Solution {
private static final 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 getPicked(int person, int problem){
int[] rule = RULES[person];
int index = problem % rule.length;
return rule[index];
}
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 = getPicked(person, problem);
if (answer == picked) {
if (++corrects[person] > max) {
max = corrects[person];
}
}
}
}
final int maxCorrects = max;
return IntStream.range(0, 3)
.filter(i -> corrects[i] == maxCorrects)
.map(i -> i + 1)
.toArray();
}
}
파이썬 코드
def solution(answers):
student1 = [1, 2, 3, 4, 5]
student2 = [2, 1, 2, 3, 2, 4, 2, 5]
student3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
score = [0, 0, 0]
result = []
for idx, answer in enumerate(answers):
if answer == student1[idx % len(student1)]:
score[0] += 1
if answer == student2[idx % len(student2)]:
score[1] += 1
if answer == student3[idx % len(student3)]:
score[2] += 1
for idx, s in enumerate(score):
if s == max(score):
result.append(idx + 1)
return result