2418. Sort the People

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public String[] sortPeople(String[] names, int[] heights) {
Integer[] indexes = new Integer[heights.length];
for (int i = 0; i < indexes.length; i++) {
indexes[i] = i;
}
Arrays.sort(indexes, (o1, o2) -> Integer.compare(heights[o2], heights[o1]));

String[] res = new String[names.length];
for (int i = 0; i < res.length; i++) {
res[i] = names[indexes[i]];
}
return res;
}
}

将索引按照身高排序,再组装结果即可。

References

2418. Sort the People