import java.util.ArrayList;
public class FailureRate {
public int[] solution(int N, int[] stages) {
ArrayList<Double> failureList = new ArrayList<>();
ArrayList<Integer> rankList = new ArrayList<>();
int size = stages.length;
int count;
for (int i = 1; i <= N; i++) {
count = 0;
for (int j = 0; j < stages.length; j++) {
if (stages[j] > 0 && stages[j] <= i) {
count++;
stages[j] = -1;
}
}
if (size == 0) {
failureList.add(0.0);
} else {
failureList.add((double) count / size);
}
size -= count;
}
for (int i = 0; i < failureList.size(); i++) {
int max = 0;
for (int j = 0; j < failureList.size(); j++) {
if (failureList.get(max) < failureList.get(j)) {
max = j;
}
}
rankList.add(max + 1);
failureList.set(max, -1.0);
}
return rankList.stream().mapToInt(i -> i.intValue()).toArray();
}
public static void main(String[] args) {
FailureRate s = new FailureRate();
int[] arr1 = { 2, 1, 2, 4, 2, 4, 3, 3 };
int[] arr2 = { 4, 4, 4, 4, 4 };
for (int i = 0; i < s.solution(5, arr1).length; i++) {
System.out.print(s.solution(5, arr1)[i] + " ");
}
System.out.println();
for (int i = 0; i < s.solution(4, arr2).length; i++) {
System.out.print(s.solution(4, arr2)[i] + " ");
}
}
}
- (NaN 처리) 0 으로 나누는 예외를 처리해주어야 한다.