Sort the People (LeetCode 2418): Solution and Explanation

Easy
Array
Hash Table
String
Sorting

Each person has a name and a distinct height. Return the names ordered from the tallest person to the shortest.

Intuition

The heights decide the order and they are all different, so there is exactly one correct answer.

Names can repeat, so a map from name to height breaks on inputs like two people called Bob. The safe link between a name and its height is the shared index i.

Sort the indices by height, tallest first, then read off the names in that order. Every name travels with its own index, so duplicates are no problem.

Approach: Sort indices by height

  1. Build order = [0, 1, ..., n - 1].
  2. Sort order so that heights[order[0]] is the largest and heights decrease from there.
  3. Create result by taking names[i] for each i in order.
  4. Return result.

Step-by-step example

names = ["Mary","John","Emma"], heights = [180,165,170]
  1. order starts as [0,1,2].
  2. Sorted by height descending: index 0 (180), index 2 (170), index 1 (165), so order = [0,2,1].
  3. Read names: Mary, then Emma, then John.
  4. Return ["Mary","Emma","John"].

Code: Python, Java, C++

1class Solution:
2    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
3        order = list(range(len(names)))
4        order.sort(key=lambda i: heights[i], reverse=True)
5        result = []
6        for i in order:
7            result.append(names[i])
8        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting by height O(n + H)
Sort indices by height O(n log n)
Map height to name O(n log n)
Sort (height, name) pairs O(n log n)
Time

O(n log n)

One comparison sort over n indices; building the result is O(n).

Space

O(n)

The index array and the result each hold n entries.

Other approaches

Map height to name (time O(n log n), space O(n))

Since heights are distinct they can be keys: store height -> name, sort the heights descending and look each one up. Works here only because heights never repeat.

Sort (height, name) pairs (time O(n log n), space O(n))

Zip the arrays into pairs, sort by height descending and keep the names. Same idea as sorting indices, written with tuples.

Counting by height (time O(n + H), space O(H))

Heights are at most 10^5, so a bucket array indexed by height can hold each name; scan it from the top. Linear in n plus the height range.

Common mistakes

  • Using a name-to-height map. Example 2 has two people named Bob, and the map keeps only one of them.
  • Sorting ascending and forgetting to reverse. The answer goes from tallest to shortest.
  • Sorting names and heights separately, which breaks the pairing between a name and its height.
  • Comparators that subtract can overflow in general; here heights are at most 10^5, so heights[b] - heights[a] is safe.

Frequently asked questions

Why sort indices instead of names?

An index is a unique handle for one person, so it carries both the name and the height. Sorting names directly would lose which height belongs to which name.

Does the sort need to be stable?

No. Stability only matters when keys tie, and all heights are distinct, so every comparison has a clear winner.

Can this be done faster than O(n log n)?

With heights bounded by 10^5, a counting approach indexed by height runs in O(n + 10^5). For n up to 1000, sorting is simpler and just as fast in practice.