




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.
1def sort_people(names, heights):
2    paired_list = list(zip(heights, names))
3    paired_list.sort(reverse=True, key=lambda x: x[0])
4    sorted_names = [name for _, name in paired_list]
5    return sorted_namesThe code creates a list of tuples, each containing a height and the corresponding name. The list is then sorted in descending order based on the height, by using a lambda function as the sorting key. Finally, the sorted names are extracted from the tuples.
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.
1using System;
2using System.Linq;
public class SortPeopleByIndexClass {
    public static string[] SortPeople(string[] names, int[] heights) {
        int[] indices = Enumerable.Range(0, names.Length).ToArray();
        Array.Sort(indices, (a, b) => heights[b].CompareTo(heights[a]));
        return indices.Select(i => names[i]).ToArray();
    }
}Create an array of indices and sort them using the heights for ordering criteria. Use LINQ to fetch and order the names based on these indices after sorting by heights in descending order.