알고리즘/프로그래머스
[프로그래머스] 실패율 문제풀이 JS
_hoji
2023. 8. 8. 01:02
stages를 탐색해서 해당 스테이지에 남아있는 사용자의 수를 stayUserCount배열에 저장한다.
이후 1부터 N까지 순회를 하면서,
해당 스테이지를 도전한 유저의 수에 남아있는 유저의 수를 나누면
해당 스테이지의 실패율을 구할 수 있다.
*해당스테이지를 클리어한 유저의 수는 초깃값은 stages의 전체길이, 즉 전체 유저의 수이고,
1부터 N까지 순회를 할때 이전 스테이지에서 남아있던 유저의 수를 빼면
현재 스테이지에 도전한 유저의 수를 구할 수 있다.
(해당 스테이지를 clear한게 아니라 시도한 유저이기 때문에 해당 스테이지에 남아있는 유저도 더해야함)
failPercentList를 채우고 난 후, failPercent를 기준으로 내림차순 정렬을 한 후,
정렬된 failPercent에서 stage의 number만 반환하도록 map을 돌리면
스테이지 별로 실패율을 계산한 결과를 얻을 수 있다
function solution(N, stages) {
let stayUserCount = new Array(N + 2).fill(0)
failPercentList = []
for (const stage of stages) {
stayUserCount[stage]++
}
let clearUserCount = stages.length
for (let n = 1; n <= N; n++) {
clearUserCount -= stayUserCount[n - 1]
failPercentList[n - 1] = {number: n, failPercent: stayUserCount[n] / clearUserCount}
}
failPercentList.sort((a, b) => b.failPercent - a.failPercent)
return failPercentList.map((stage) => stage.number)
}
https://school.programmers.co.kr/learn/courses/30/lessons/42889