PS/Programmers

프로그래머스 모의고사 / Lv1 / JAVA

얍연구소장 2023. 4. 17.

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

 

프로그래머스

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

programmers.co.kr

 

> 풀이

 

import java.util.*;

class Solution {
    
    public int[] solution(int[] answers) {
        int answr[] = new int[3];
        
        int[][] supoza = {
            {1,2,3,4,5},
            {2,1,2,3,2,4,2,5},
            {3,3,1,1,2,2,4,4,5,5}
        };
        
        int[] correct = new int[3];
        for(int i = 0; i < correct.length; i++) {
            for(int j = 0; j < answers.length; j++) {
                int select = supoza[i][j % supoza[i].length];
                
                // select : 수포자가선택한 답, answers[i] : 실제 답
                if(select == answers[j]) {
                    correct[i]++;
                }
            }
        }
        
        // 가장 많이 맞춘 답 갯수
        int maxCnt = Math.max(correct[0], Math.max(correct[1], correct[2]));
        
        // 가장많이맞춘 답 갯수가 동일한 수포자 실제 인덱스 구하기
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0; i < correct.length; i++) {
            if(maxCnt == correct[i]) {
                list.add(i+1); // 수포자 번호와 맞춰주기 위해 +1
            }
        }
        
        // 배열 형태로 리스트에 들어있는 수포자 인덱스 대입
        int[] answer = new int[list.size()];
        int idx = 0;
        for(int x : list) {
            answer[idx++] = x;
        }
        return answer;
    }
}

댓글