class Solution {
public static int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] person1, int[] person2) {
if (person1[0] != person2[0]) {
return person2[0] - person1[0];
} else {
return person1[1] - person2[1];
}
}
});
List<int[]> ans = new ArrayList<int[]>();
for (int[] person : people) {
ans.add(person[1], person);
}
return ans.toArray(new int[ans.size()][]);
}
}
class Solution {
public static int[][] reconstructQueue(int[][] people) {
if (people.length == 0) {
return people;
}
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[1] == o2[1]) {
return o1[0]-o2[0];
}
return o1[1] - o2[1];
}
});
List<int []> list = new LinkedList<>();
list.add(0, people[0]);
for (int i = 1; i < people.length; i++) {
int maxs = 0;
int size = list.size();
for (int j = 0; j < size; j++) {
if (people[i][0] <= list.get(j)[0]) {
maxs++;
}
if (people[i][1] == maxs-1) {
list.add(j, people[i]);
break;
}
if (j == list.size()-1) {
list.add(j+1, people[i]);
break;
}
}
}
int [][]result = new int[people.length][2];
int size = list.size();
for (int i = 0; i < size; i++) {
result[i] = list.get(i);
}
return result;
}
}