DDSA
Advertisement

2418. Sort the People

Time: O(n log n)
Space: O(n)

Approach

Pair (height, name) and sort by height descending; extract names.

2418.cs
C#
// Approach: Pair (height, name) and sort by height descending; extract names.
// Time: O(n log n) Space: O(n)

public class Solution
{
    public string[] SortPeople(string[] names, int[] heights)
    {
        List<KeyValuePair<int, string>> heightAndNames = new List<KeyValuePair<int, string>>();

        for (int i = 0; i < names.Length; ++i)
            heightAndNames.Add(new KeyValuePair<int, string>(heights[i], names[i]));

        heightAndNames.Sort((a, b) => b.Key.CompareTo(a.Key));

        for (int i = 0; i < heightAndNames.Count; ++i)
            names[i] = heightAndNames[i].Value;

        return names;
    }
}
Advertisement
Was this solution helpful?