https://programmers.co.kr/learn/courses/30/lessons/42840
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] type1 = {1, 2, 3, 4, 5};
int[] type2 = {2, 1, 2, 3, 2, 4, 2, 5};
int[] type3 = {3, 3, 1, 1, 2, 2 ,4, 4, 5, 5};
int[] temp = new int[3];
for (int i = 0; i < answers.length; i++) {
if (type1[i % 5] == answers[i]) {
temp[0]++;
}
if (type2[i % 8] == answers[i]) {
temp[1]++;
}
if (type3[i % 10] == answers[i]) {
temp[2]++;
}
}
int max = Math.max(Math.max(temp[0], temp[1]), temp[2]);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
if (temp[i] == max) {
list.add(i + 1);
}
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
문제자체는 쉬웠지만 마지막에 ArrayList<Integer>
를 int[]
로 변환하는과정을 stream
으로 변환했다. stream
을 공부해야겠다고 생각했고 Python
으로 했으면 엄청쉽게 리턴할수있는데 싶었다..