https://school.programmers.co.kr/learn/courses/30/lessons/42889
- 스테이지별로 인원수를 담는 배열을 설정한다.
(stages에 N + 1까지의 숫자가 담겨있기 때문에 N + 2로 선언)int people[] = new int[N + 2];
- list에 해당 stage번호와 그의 실패율을 넣어준다.
for(int i = 1; i <= N; i++) {
double fail = 0;
if(length >= 1)
fail = (double) people[i] / length;
list.add(new Node(fail, i));
length -= people[i];
}
- 실패율이 높은 스테이지부터 내림차순.
Collections.sort(list, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
if(o1.fail == o2.fail)
return Integer.compare(o1.num, o2.num);
return Double.compare(o2.fail, o1.fail);
}
});
- 내림차순으로 스테이지 번호가 담긴 배열을 return한다.
int answer[] = new int[N];
for(int a = 0; a < N; a++)
answer[a] = list.get(a).num;
return answer;
import java.util.*;
class Solution {
public int[] solution(int N, int[] stages) {
int people[] = new int[N + 2];
for(int i = 0; i < stages.length; i++) {
people[stages[i]] += 1;
}
int length = stages.length;
ArrayList<Node> list = new ArrayList<Node>();
for(int i = 1; i <= N; i++) {
double fail = 0;
if(length >= 1)
fail = (double) people[i] / length;
list.add(new Node(fail, i));
length -= people[i];
}
Collections.sort(list, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
if(o1.fail == o2.fail)
return Integer.compare(o1.num, o2.num);
return Double.compare(o2.fail, o1.fail);
}
});
int answer[] = new int[N];
for(int a = 0; a < N; a++)
answer[a] = list.get(a).num;
return answer;
}
public static class Node {
double fail;
int num;
public Node(double fail, int num) {
this.fail = fail;
this.num = num;
}
}
}