문제 설명
해결과정
Solution.java
import java.util.*;
class Solution {
public int[] solution(int N, int[] stages) {
int[] answer = new int[N];
double[] fail = new double[N];
Map<Integer, Integer> map = arrmap(stages);
Arrays.sort(stages);
// arrstr(stages);
int personnel = stages.length;
double maxfail = 0;
for(int i = 0; i < fail.length; i++) {
if(map.containsKey(i+1)) {
int cnt = map.get(i+1);
// System.out.println(((double)cnt / (double)personnel));
fail[i] = (double)cnt / (double)personnel;
personnel -= cnt;
} else {
fail[i] = 0;
// System.out.println(((double)0 / (double)personnel));
}
maxfail = maxfail > fail[i] ? maxfail : fail[i];
}
System.out.println(maxfail);
double nextfail = 0;
int index = 0;
while(index < answer.length) {
for(int i = 0; i < fail.length; i++) {
if(fail[i] < maxfail) {
nextfail = nextfail > fail[i] ? nextfail : fail[i];
} else if (fail[i] == maxfail) {
answer[index++] = i+1;
}
}
maxfail = nextfail;
nextfail = 0;
}
// arrstr(fail);
// System.out.println(map.toString());
return answer;
}
public Map<Integer, Integer> arrmap(int [] stages) {
Map<Integer, Integer> map = new HashMap<>();
for(int stage : stages) {
if(map.containsKey(stage)) {
int cnt = map.get(stage);
map.put(stage, cnt+1);
} else {
map.put(stage, 1);
}
}
return map;
}
public void arrstr(int [] stages) {
for(int stage : stages) {
System.out.print(stage + " ");
}
System.out.println();
}
public void arrstr(double [] stages) {
for(double stage : stages) {
System.out.print(stage + " ");
}
System.out.println();
}
}
Java
복사