주의 1 : 결과는 76-24-3으로 정렬이 아니라
첫번째는 1번, 두번째는 1번, 세번째는 2번
인덱스는 고정시켜두고 순위를 작성한다
주의 2 : 순위이므로 0부터 시작하는 idx+1
v로 정렬시키고, i에 따른 3 1 2를
순위로 줄 세우고, 그 순위만 챙겨서 다시 되돌리기
순위로 줄 세웠다면, 갱신된 sorted배열의 idx는 순위가 된다. 그럼 value는 필요없어짐
import java.util.Arrays;
class Solution {
public int[] solution(int[] emergency) {
int L = emergency.length;
// value기준 순위 얻기
Node[] sorted = new Node[L];
for (int i = 0; i < L; i++) {
sorted[i] = new Node(i, emergency[i]);
}
Arrays.sort(sorted);
// value기준 순위와 원본 index 매칭
Node[] sorted2 = new Node[L];
for (int i = 0; i < L; i++) {
sorted2[i] = new Node(i, sorted[i].index);
}
Arrays.sort(sorted2);
int[] ans = new int[L];
for (int i = 0; i < L; i++) {
ans[i] = 1 + sorted2[L-1-i].index;
}
return ans;
}
}
class Node implements Comparable<Node> {
int index;
int value;
public Node(int index, int value) {
super();
this.index = index;
this.value = value;
}
@Override
public int compareTo(Node n) {
return n.value - this.value;
}
}