/**
* 모의고사
* 완전탐색
*/
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 배열에 추가만 해주면 결과가 나오도록 코드를 구성했다.
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스][자바스크립트][Level2] 프린터 (0) | 2022.01.03 |
---|---|
[프로그래머스][자바스크립트][Level2] 소수 찾기 (0) | 2022.01.03 |
[프로그래머스][자바스크립트][Level2] 카펫 (0) | 2022.01.03 |
[프로그래머스][자바스크립트][Level1][카카오] 숫자 문자열과 영단어 (0) | 2022.01.02 |
[프로그래머스][자바스크립트][Level1] 로또의 최고 순위와 최저 순위 (0) | 2022.01.01 |
댓글