[programmers] 모의고사

KwonSC·2022년 4월 13일
0

programmers - Java

목록 보기
15/17
post-thumbnail

https://programmers.co.kr/learn/courses/30/lessons/42840


Code

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();
    }
}

Solution

문제자체는 쉬웠지만 마지막에 ArrayList<Integer>int[]로 변환하는과정을 stream으로 변환했다. stream을 공부해야겠다고 생각했고 Python으로 했으면 엄청쉽게 리턴할수있는데 싶었다..

0개의 댓글