2418. Sort the People

题目 #

You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

For each index i, names[i] and heights[i] denote the name and height of the ith person.

Return names sorted in descending order by the people’s heights.

Example 1:

Input: names = ["Mary","John","Emma"], heights = [180,165,170]
Output: ["Mary","Emma","John"]
Explanation: Mary is the tallest, followed by Emma and John.

Example 2:

Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
Output: ["Bob","Alice","Bob"]
Explanation: The first Bob is the tallest, followed by Alice and the second Bob.

Constraints:

  • n == names.length == heights.length
  • $1 \le n \le 10^3$
  • $1 \le names[i].length \le 20$
  • $1 \le heights[i] \le 10^5$
  • names[i] consists of lower and upper case English letters.
  • All the values of heights are distinct.

思路1 排序 #

分析 #

  • 使用go的sort,写一个对应的类型,调用Sort排序就好了

代码实现 #

 1type NameSlice struct {
 2	names   []string
 3	heights []int
 4}
 5
 6func (x NameSlice) Len() int           { return len(x.names) }
 7func (x NameSlice) Less(i, j int) bool { return x.heights[i] > x.heights[j] }
 8func (x NameSlice) Swap(i, j int) {
 9	x.heights[i], x.heights[j] = x.heights[j], x.heights[i]
10	x.names[i], x.names[j] = x.names[j], x.names[i]
11}
12
13func sortPeople(names []string, heights []int) []string {
14	tmp := NameSlice{
15		names:   names,
16		heights: heights,
17	}
18	sort.Sort(tmp)
19	return tmp.names
20}