
https://school.programmers.co.kr/learn/courses/30/lessons/120835
배열 요소값이 클 수록 응급도가 높다.
하지만 주어진 원본 배열 순서대로 응급도 순서를 넣어주어야 하므로
원본 배열을 정렬해야겠다는 옵션은 버렸다.
그래서 원본을 카피한 카피배열을 정렬하고 (문법 외우기)
int[] copy = Arrays.copyOf(emergency, emergency.length); // 배열 전체 복사
이를 해시맵에 담아서 최종적으로 for문에서 요소를 키로 해서 응급도를 받아서 배열을 완성했다.
로직과 구조도 다 맞았는데 map에 value 넣는 로직 copy.length - i를 못 떠올려서 헤맸던게 아쉽다.
i배열.length - iimport java.util.*;
class Solution {
public int[] solution(int[] emergency) {
int[] copy = Arrays.copyOf(emergency, emergency.length);
Arrays.sort(copy);
// map에 우선순위를 먼저 정해줄거야.
Map<Integer, Integer> map = new HashMap<>();
for(int i = copy.length - 1 ; i >=0; i--) {
map.put(copy[i], copy.length - i);
}
int[] answer = new int[emergency.length];
for(int i = 0; i < emergency.length; i++) {
answer[i] = map.get(emergency[i]);
}
return answer;
}
}