Algorithm
프로그래머스
Java
모의고사

42840 모의고사

https://school.programmers.co.kr/learn/courses/30/lessons/42840?language=java (opens in a new tab)

풀이

  • 주요문법

int max = Arrays.stream(scores).max().getAsInt();

import java.util.*;
 
class Solution {
    public int[] solution(int[] answers) {
        int[] scores = {0,0,0};
        int[][] picks = {{1,2,3,4,5}, {2,1,2,3,2,4,2,5}, {3, 3, 1, 1, 2, 2, 4, 4, 5, 5}};
        for(int i = 0; i < answers.length; i ++) {
            if(picks[0][i % 5] == answers[i]) {
                scores[0] ++;
            }
 
            if(picks[1][i % 8] == answers[i]) {
                scores[1] ++;
            }
 
            if(picks[2][i % 8] == answers[i]) {
                scores[2] ++;
            }
		}
        int max = Arrays.stream(scores).max().getAsInt();
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        for(int i = 0; i < scores.length; i++) {
            if(scores[i] == max) {
                tmp.add(i);
            }
        }
        int[] ans = tmp.stream().mapToInt(Integer::intValue).toArray();
 
        return ans;
    }
}