2418. Sort the People LeetCode Solution

In this guide, you will get 2418. Sort the People LeetCode Solution with the best time and space complexity. The solution to Sort the People problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sort the People solution in C++
  4. Sort the People solution in Java
  5. Sort the People solution in Python
  6. Additional Resources
2418. Sort the People LeetCode Solution image

Problem Statement of Sort the People

You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.
For each index i, names[i] and heights[i] denote the name and height of the ith person.
Return names sorted in descending order by the people’s heights.

Example 1:

Input: names = [“Mary”,”John”,”Emma”], heights = [180,165,170]
Output: [“Mary”,”Emma”,”John”]
Explanation: Mary is the tallest, followed by Emma and John.

Example 2:

Input: names = [“Alice”,”Bob”,”Bob”], heights = [155,185,150]
Output: [“Bob”,”Alice”,”Bob”]
Explanation: The first Bob is the tallest, followed by Alice and the second Bob.

Constraints:

n == names.length == heights.length
1 <= n <= 103
1 <= names[i].length <= 20
1 <= heights[i] <= 105
names[i] consists of lower and upper case English letters.
All the values of heights are distinct.

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

2418. Sort the People LeetCode Solution in C++

class Solution {
 public:
  vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
    vector<string> ans;
    vector<pair<int, string>> heightAndNames;

    for (int i = 0; i < names.size(); ++i)
      heightAndNames.emplace_back(heights[i], names[i]);

    ranges::sort(heightAndNames, greater<>());

    for (const auto& [_, name] : heightAndNames)
      ans.push_back(name);

    return ans;
  }
};
/* code provided by PROGIEZ */

2418. Sort the People LeetCode Solution in Java

class Solution {
  public String[] sortPeople(String[] names, int[] heights) {
    List<Pair<Integer, String>> heightAndNames = new ArrayList<>();

    for (int i = 0; i < names.length; ++i)
      heightAndNames.add(new Pair<>(heights[i], names[i]));

    Collections.sort(heightAndNames, (a, b) -> b.getKey().compareTo(a.getKey()));

    for (int i = 0; i < heightAndNames.size(); ++i)
      names[i] = heightAndNames.get(i).getValue();

    return names;
  }
}
// code provided by PROGIEZ

2418. Sort the People LeetCode Solution in Python

class Solution:
  def sortPeople(self, names: list[str], heights: list[int]) -> list[str]:
    return [height for _, height in
            sorted([(height, name) for name, height in zip(names, heights)], reverse=True)]
# code by PROGIEZ

Additional Resources

See also  1845. Seat Reservation Manager LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.