-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
42 lines (42 loc) · 1.33 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
public int[][] reconstructQueue(int[][] people) {
//按照k排序,k小的放前面,k相同h小的放前面
Comparator<int[]> c=new Comparator<int[]>() {
public int compare(int[] arr1, int[] arr2) {
if(arr1[1] == arr2[1])
return arr1[0] - arr2[0];
else
return arr1[1] - arr2[1];
}
};
Arrays.sort(people,c);
List<int[]> list=new ArrayList<int[]>();
for (int i = 0; i < people.length; i++) {
if (i==0) {
list.add(people[i]);
}else{
//定位
int h=people[i][0];
int k=people[i][1];
int j=0;
boolean flag=true;
while(j<list.size()&&k>=0&&flag){
if (list.get(j)[0]>=h) {
if (k==0) {
flag=false;
break;
}
k--;
}
j++;
}
list.add(j, people[i]);
}
}
int[][] ans=new int[list.size()][2];
for (int i = 0; i < list.size(); i++) {
ans[i]=list.get(i);
}
return ans;
}
}