프로그래머스 LEVEL 1(모의고사)
-
사용 언어 : javascript
-
해결 날짜 : 2022-08-24
-
해결 방법 :
- answers 배열을 돌며 각 수포자별로 정답을 맞춘 경우 1씩 증가
- 세 수포자 중 가장 높은 점수를 받은 값의 인덱스를 반환
-
회고 : -
-
코드
function solution(answers) { const counts = [0, 0, 0]; const answer = []; const first = [1, 2, 3, 4, 5]; const second = [2, 1, 2, 3, 2, 4, 2, 5]; const third = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]; for (const [index, ans] of answers.entries()) { if (first[index % first.length] === ans) counts[0]++; if (second[index % second.length] === ans) counts[1]++; if (third[index % third.length] === ans) counts[2]++; } const max = Math.max(...counts); var idx = counts.indexOf(max); while (idx != -1) { answer.push(idx + 1); idx = counts.indexOf(max, idx + 1); } return answer; }
-
출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges