본문 바로가기

Tech/[PS] Reviews

[프로그래머스-코딩테스트 연습] 모의고사

Hi, There!
안녕하세요, 바오밥입니다.


목차

  • 문제
  • 풀이

 


문제

문제 내용

https://school.programmers.co.kr/learn/courses/30/lessons/42840?language=java

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

풀이

나의 풀이

- 쉽게 브루트포스로 바로 풀이하였다.

import java.util.*;

class Solution {
    public int[] solution(int[] answers) {
//         가장 문제 많이 맞춘 사람
        int[] first = {1, 2, 3, 4, 5};
        int[] second = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] third = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int[] score = {0, 0, 0};
        
        for(int i=0; i<answers.length; i++) {
            if(answers[i] == first[i%5]) score[0]++;
            if(answers[i] == second[i%8]) score[1]++;
            if(answers[i] == third[i%10]) score[2]++;
        }
        
        int max = Math.max(Math.max(score[0], score[1]), score[2]);
        ArrayList<Integer> intAL = new ArrayList<>();
        
        for(int i=0; i<score.length; i++) if(score[i]==max) intAL.add(i+1);
        
        return intAL.stream().mapToInt(x->x).toArray();
    }
}