ArrayList<Integer> keySet = new ArrayList<Integer>(failRatio.keySet());
Collections.sort( keySet,(o1, o2) -> (failRatio.get(o1).compareTo(failRatio.get(o2))) );
->참고로 value 값이 같으면 key값이 작은것부터 정렬됨
ArrayList<Integer> keySet = new ArrayList<Integer>(failRatio.keySet());
Collections.sort( keySet,(o1, o2) -> (failRatio.get(o2).compareTo(failRatio.get(o1))) );
->참고로 value 값이 같으면 key값이 작은것부터 정렬됨
import java.util.*;
class Solution {
public int[] solution(int N, int[] stages) {
int[] answer = new int[N];
double motherNum = stages.length;
double failNum = 0;
HashMap<Integer, Double> failRatio = new HashMap<Integer, Double>();
for(int i = 0; i < N; i++){
failNum = 0;
motherNum = stages.length;
for(int now : stages){
if(now < i+1) motherNum--;
if(i+1 == now){
failNum++;
}
}
if(motherNum != 0)
failRatio.put(i+1, failNum/motherNum);
else
failRatio.put(i+1, 0.0);
}
ArrayList<Integer> keySet = new ArrayList<Integer>(failRatio.keySet());
Collections.sort( keySet,(o1, o2) -> (failRatio.get(o2).compareTo(failRatio.get(o1))) );
int k = 0;
for(int now : keySet){
answer[k] = now;
k++;
}
return answer;
}
}
-> 깔끔하게 성공 ㅎ
해당 스테이지까지 못간 경우를 처음에 생각 못해서 분모가 0인 경우가 잘못 계산되었는데
if(motherNum != 0)
failRatio.put(i+1, failNum/motherNum);
else
사용해서 해결했음