406 Queue Reconstruction by Height
1. Question
Input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
Output: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]2. Implementation
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
}
});
List<int[]> list = new ArrayList<>();
for (int[] info : people) {
list.add(info[1], info);
}
int[][] res = new int[people.length][2];
for (int i = 0; i < people.length; i++) {
res[i][0] = list.get(i)[0];
res[i][1] = list.get(i)[1];
}
return res;
}
}3. Time & Space Complexity
Last updated