




Sponsored
Sponsored
This approach involves pairing each name with its corresponding height, sorting these pairs by height in descending order, and then extracting the sorted names. This makes use of the association between each name and its corresponding height.
Time Complexity: O(n log n) due to the sorting operation.
Space Complexity: O(n) for storing the list of tuples.
1using System;
2using System.Linq;
3
4public class SortPeopleClass {
5    public static string[] SortPeople(string[] names, int[] heights) {
6        var people = names.Zip(heights, (name, height) => new { Name = name, Height = height });
7        return people.OrderByDescending(p => p.Height).Select(p => p.Name).ToArray();
8    }
9}Use LINQ to pair names with heights, then apply OrderByDescending to sort by height in descending order. Finally, project the sorted names from the query result.
Rather than pairing names and heights, you can directly sort the indices of the height array. After sorting the indices by descending heights, you can retrieve the names based on this sorted order of heights.
Time Complexity: O(n log n) due to sorting the indices.
Space Complexity: O(n) for auxiliary storage of indices.
1defThe code sorts indices based on the heights using negative heights to direct the sorting to descending order. After sorting, use these indices to create the sorted list of names.