👍 2022년 1월 23일
<script>
function solution(N, stages) {
var answer = [];
let failRate = [];
for (let stage = 1; stage<=N; stage++) {
let totalChallengers = 0; // 해당 스테이지 도전자 수
let notClear = 0; // 해당 스테이지 클리어 못한 사람 수
stages.forEach((v,i)=> {
if (v >= stage) totalChallengers++;
if (v === stage) notClear++;
})
totalChallengers === 0 ? failRate.push(0) : failRate.push(notClear / totalChallengers);
}
while(true) {
let maxRatio = Math.max(...failRate); // 가장 큰 실패율
let idx = failRate.indexOf(maxRatio); // 가장 큰 실패율의 인덱스
if (maxRatio === -1) break
answer.push(idx+1);
failRate[idx] = -1;
}
return answer;
}
</script>
<script>
function solution(N, stages) {
let failRate = [];
for (let stage = 1; stage<=N; stage++) {
let totalChallengers = 0; // 해당 스테이지 도전자 수
let notClear = 0; // 해당 스테이지 클리어 못한 사람 수
stages.forEach((v,i)=> {
if (v >= stage) totalChallengers++;
if (v === stage) notClear++;
})
totalChallengers === 0
? failRate.push([stage,0])
: failRate.push([stage, notClear / totalChallengers]);
}
// 최고..!!!
failRate.sort((a,b) => b[1] - a[1]);
return failRate.map((x) => x[0]);
}
</script>
실패율 계산까지는 쉽게 했는데 내림차순으로 정렬하고 해당 스테이지를 뽑아내는게 도저히 생각이 안났다... 어찌저찌 해결하고나서 다른 사람의 풀이 봤는데 내가 딱 원하던게 있어서 많이 배웠다고 댓글도 달았다.