Leetcode 2418: Sort the People

grid47
grid47
Exploring patterns and algorithms
Mar 10, 2024 4 min read

Given two arrays, one containing the names and the other containing the corresponding heights of people, return the names sorted in descending order by their heights.
Problem
Approach
Steps
Complexity
Input: You are given two arrays: one for the names and one for the heights of n people.
Example: names = ["Tom", "Jerry", "Mickey"], heights = [170, 160, 175]
Constraints:
• 1 <= n <= 1000
• 1 <= names[i].length <= 20
• 1 <= heights[i] <= 10^5
• names[i] consists of lower and upper case English letters.
• All heights are distinct.
Output: Return the names of the people sorted in descending order by height.
Example: Output: ["Mickey", "Tom", "Jerry"]
Constraints:
Goal: Sort the people by their heights in descending order while maintaining the correct mapping of names.
Steps:
• 1. Create a list of pairs, each containing a height and a corresponding name.
• 2. Sort the pairs in descending order by height.
• 3. Extract and return the sorted names.
Goal: The solution should handle sorting and efficiently process up to 1000 names and heights.
Steps:
• The number of people (n) is at most 1000.
• The heights array contains distinct values.
Assumptions:
• The names array contains distinct names and corresponds to the heights array.
Input: Input: names = ["Tom", "Jerry", "Mickey"], heights = [170, 160, 175]
Explanation: The heights are sorted as [175, 170, 160], so the corresponding names are sorted as ["Mickey", "Tom", "Jerry"].

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus