알고리즘 풀이/프로그래머스
[level1] 프로그래머스 - 모의고사(JAVA)
데롱디롱
2021. 8. 27. 01:46
728x90
- 1번, 2번, 3번 수포자의 루틴을 담은 배열을 만든다.
- count[]에 각자가 몇 문제를 맞췄는지 저장한다.
- 1~3번 수포자 중에 가장 문제를 많이 맞춘 수를 max로 정한다.
- answer의 크기를 어떻게 해야되는지.. 감이 안와서, 일단 ArrayList에 넣고 이를 다시 answer에 넣는 방법으로 하였다.
import java.util.ArrayList;
class Solution {
public int[] solution(int[] answers) {
int[] one = { 1, 2, 3, 4, 5 };
int[] two = { 2, 1, 2, 3, 2, 4, 2, 5 };
int[] three = { 3, 3, 1, 1, 2, 2, 4, 4, 5, 5 };
int count[] = new int[4];
for (int i = 0; i < answers.length; i++) {
count[1] += answers[i] == one[i % 5] ? 1 : 0;
count[2] += answers[i] == two[i % 8] ? 1 : 0;
count[3] += answers[i] == three[i % 10] ? 1 : 0;
}
int max = Math.max(count[1], Math.max(count[2], count[3]));
ArrayList<Integer> num = new ArrayList<Integer>();
for (int i = 1; i < count.length; i++)
if (count[i] == max)
num.add(i);
int[] answer = new int[num.size()];
for (int i = 0; i < num.size(); i++)
answer[i] = num.get(i);
return answer;
}
}