코딩 테스트/프로그래머스 level1
모의고사
fullfish
2022. 9. 8. 02:56
코드
function solution(answers) {
let stu1 = [1, 2, 3, 4, 5];
let stu2 = [2, 1, 2, 3, 2, 4, 2, 5];
let stu3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
let scoreArr = [0, 0, 0];
let resultArr = [];
for (let i = 0; i < answers.length; i++) {
if (answers[i] === stu1[i % 5]) scoreArr[0]++;
if (answers[i] === stu2[i % 8]) scoreArr[1]++;
if (answers[i] === stu3[i % 10]) scoreArr[2]++;
}
let max = Math.max(...scoreArr);
while (scoreArr.indexOf(max, 0) !== -1) {
resultArr.push(scoreArr.indexOf(max, 0) + 1);
scoreArr[scoreArr.indexOf(max, 0)] = -1;
}
return resultArr;
}
풀고보니 굳이 while내에서 max값 확인한것을 -1넣어줄 필요가 없었다
어차피 순위 메기기가 아닌 최고점자 찾기이며 동점자의 경우 오름차순이므로
function solution(answers) {
let stu1 = [1, 2, 3, 4, 5];
let stu2 = [2, 1, 2, 3, 2, 4, 2, 5];
let stu3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
let scoreArr = [0, 0, 0];
let resultArr = [];
for (let i = 0; i < answers.length; i++) {
if (answers[i] === stu1[i % 5]) scoreArr[0]++;
if (answers[i] === stu2[i % 8]) scoreArr[1]++;
if (answers[i] === stu3[i % 10]) scoreArr[2]++;
}
let max = Math.max(...scoreArr);
for (let i = 0; i < scoreArr.length; i++) {
if (scoreArr[i] === max) resultArr.push(i + 1);
}
return resultArr;
}
이게 좀더 깔끔하다