본문 바로가기
알고리즘/프로그래머스

[프로그래머스][자바스크립트][Level1] 모의고사

by Benjamin_Choi 2022. 1. 3.

/**
 * 모의고사
 * 완전탐색
 */

 function solution(answers) {
    const supoja = [0, 0, 0];
    const supojaPattern = [[1, 2, 3, 4, 5], [2, 1, 2, 3, 2, 4, 2, 5], [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]];
    
    answers.forEach((val, idx) => {
        for (let i = 0; i < supojaPattern.length; i++) {
            if (val === supojaPattern[i][idx%supojaPattern[i].length]) supoja[i]++;
        }
    });
    
    const max = Math.max(...supoja);
    return supoja.reduce((rst, matchNum, idx) => {
        if (matchNum === max) rst.push(idx+1);
        return rst;
    }, []);
}

console.log(solution([1,2,3,4,5]));
console.log(solution([1,3,2,4,2]));

문제에선 수포자가 3명인 경우가 주어졌는데, 추후 수포자가 100명 1000명으로 늘어나도 해당 정보를 supoja와 supojaPattern 배열에 추가만 해주면 결과가 나오도록 코드를 구성했다.

댓글