20220131_알고리즘

Minseok-Choi·2022년 1월 31일
0

알고리즘

목록 보기
9/13
post-thumbnail

프로그래머스 - 실패율

import java.util.*;
class Solution {
    
    static class Stage {

        int number; // 스테이지 번호
        double rate; // 실패율

        Stage(int number, double rate) {
            this.number = number;
            this.rate = rate;
        }


    }
    
    public int[] solution(int N, int[] stages) {
       int[] stageUserNumber = new int[N+1];

        for (int i = 0; i <stages.length ; i++) {
            if(stages[i] == N+1) { // N+1은 최고스테이지 클리어
                continue;
            }
            stageUserNumber[stages[i]]++;
        }
        int total = stages.length;
        List<Stage> stagesInfo = new ArrayList<>();
        for (int i = 1; i <stageUserNumber.length ; i++) {
            int temp = stageUserNumber[i];
            if(temp == 0) { // 스테이지내에 머무르는 유저가 없으면 실패율 0
                stagesInfo.add(new Stage(i, 0));
                continue;
            }
            stagesInfo.add(new Stage(i, (double) temp/total));
            total -= temp;

        }

        Collections.sort(stagesInfo, (o1, o2) -> Double.compare(o2.rate,o1.rate));

        return stagesInfo.stream().mapToInt(s -> s.number).toArray();
    }
}
profile
차곡차곡

0개의 댓글