‡ CODING TEST STUDY ‡/º 프로그래머스

[프로그래머스 | Java Lv.1] 모의고사 (완전 탐색)

Trudy | 송연 2024. 6. 11. 03:47

문제

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

 

프로그래머스

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

programmers.co.kr


 

코드가 좀 많이 비효율적인 것 같지만....

 

최종코드

package week6.baek.exhaustiveSearch;

import java.util.ArrayList;

public class MockTest {
    public static ArrayList solution(int[] answers) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        int[] score = new int[3];

        int[][] sch = {{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++) {
            for(int j = 0; j < 3; j++) {
                if(answers[i] == sch[j][i % sch[j].length]) {
                    score[j]++;
                }
            }
        }

        int max = score[0];
        for (int i = 1; i < 3; i++) {
            if(score[i] > max) {
                max = score[i];
            }
        }

        for (int i = 0; i < 3; i++) {
            if(score[i] == max) {result.add(i + 1);}
        }

        return result;
    }
    public static void main(String[] args) {
        int[] answers = {1,3,2,4,2};
        System.out.println(solution(answers));
    }
}