https://leetcode.com/problems/queue-reconstruction-by-height/
- Queue
- Greedy
Sort by height descending and insert each person at their k index in the result list.
O(n^2)
O(n)
import java.util.*;
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
List<int[]> res = new ArrayList<>();
for (int[] p : people) res.add(p[1], p);
return res.toArray(new int[res.size()][]);
}
}