




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.
1function sortPeople(names, heights) {
2    const people = names.map((name, index) => ({ name, height: heights[index] }));
3    people.sort((a, b) => b.height - a.height);
4    return people.map(person => person.name);
5}Create an array of objects where each object has a name and height property. Sort this array by the height property in descending order. Finally, map the sorted objects back to an array of names.
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.
1The 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.