-
[Programmers] 실패율STUDYING/Algorithm 2021. 9. 27. 00:39728x90
https://programmers.co.kr/learn/courses/30/lessons/42889
import java.util.Arrays; class Solution { public static int[] solution(int N, int[] stages) { int[] answer = new int[N]; Fail[] tmpAns = new Fail[N]; double[] noClear = new double[N]; for (int stage: stages){ if (stage > N) continue; noClear[stage - 1]++; } double[] player = new double[N]; for (int i = 0; i < N; i++) { if (i == 0) player[0] = stages.length; else player[i] = player[i - 1] - noClear[i - 1]; } for (int i = 0; i < N; i++) { double percentage; if (noClear[i] == 0) percentage = 0.0; else percentage = noClear[i] / player[i]; tmpAns[i] = new Fail(i + 1, percentage); } Arrays.sort(tmpAns); for (int i = 0; i < N; i++) { answer[i] = tmpAns[i].idx; } return answer; } static class Fail implements Comparable<Fail>{ int idx; double percent; public Fail(int idx, double percent) { this.idx = idx; this.percent = percent; } @Override public int compareTo(Fail o) { if (this.percent == o.percent) { return Integer.compare(this.idx, o.idx); } else { return -Double.compare(this.percent, o.percent); } } } }
'STUDYING > Algorithm' 카테고리의 다른 글
[Programmers] 가장 큰 수 (0) 2021.09.27 [Programmers] 프린터 (0) 2021.09.27 [Programmers] 가장 먼 노드 (0) 2021.09.27 [Programmers] 타겟 넘버 (0) 2021.09.27 [Programmers] 체육복 (0) 2021.09.27