TIL
[코테연습] 2418. Sort the People
크라00
2024. 7. 22. 09:51
- 정렬문제
- 문제분석
1. 사람 이름이 담긴 String 배열과 키에 대한 정보가 담긴 int 배열이 있다.
2. 키 순으로 사람이름을 정렬해라.
- 문제풀이
1. Map<Integer, String> 을 선언하여 사람 이름과 키에 대한 정보를 담는다.
2. PriorityQueue 를 선언하여 키를 sorting 하여 담는다.
3. PriorityQueue 를 반복문으로 반복하여 새로운 String 배열을 초기화하여 map.get(queue.poll()) 하여 String배열에 담는다.
class Solution {
public String[] sortPeople(String[] names, int[] heights) {
int n = heights.length;
PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2) -> o2-o1);
Map<Integer, String> map = new HashMap<>();
for(int i = 0; i < n; i++) {
map.put(heights[i], names[i]);
queue.offer(heights[i]);
}
String[] result = new String[n];
int idx = 0;
while(!queue.isEmpty()) {
result[idx++] = map.get(queue.poll());
}
return result;
}
}