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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 3
    character(len=20), dimension(n) :: names
    integer, dimension(n) :: heights
    integer :: i

    ! Example 1
    names = ["Mary ", "John ", "Emma "]
    heights = [180, 165, 170]
    call sort_by_height(names, heights)
    write (*, "(A)") trim(names(1)) // " " // trim(names(2)) // " " // trim(names(3))

    ! Example 2
    names = ["Alice ", "Bob ", "Bob "]
    heights = [155, 185, 150]
    call sort_by_height(names, heights)
    write (*, "(A)") trim(names(1)) // " " // trim(names(2)) // " " // trim(names(3))

contains

    subroutine sort_by_height(names, heights)
        implicit none
        character(len=*), intent(inout) :: names(:)
        integer, intent(inout) :: heights(:)
        integer :: i, j

        do i = 1, size(names)-1
            do j = i+1, size(names)
                if (heights(j) < heights(i)) then
                    call swap(names(i), names(j))
                    call swap(heights(i), heights(j))
                end if
            end do
        end do
    end subroutine sort_by_height

    subroutine swap(a, b)
        implicit none
        character(len=*), intent(inout) :: a, b
        character(len=len(a)) :: temp

        temp = a
        a = b
        b = temp
    end subroutine swap

end program main
Compiled
Executed
⚠️ Correct
program sort_by_height
      implicit none

      integer, parameter :: n = 3
      character(len=20) :: names(n)
      integer :: heights(n)
      integer :: i

      names = ["Mary ", "John ", "Emma "]
      heights = [180, 165, 170]

      call sort_by_height_aux(names, heights, n)

      do i = 1, n
          write (*,*) names(i), heights(i)
      end do

      contains

      subroutine sort_by_height_aux(names, heights, n)
          implicit none
          character(len=20), intent(inout) :: names(n)
          integer, intent(inout) :: heights(n)
          integer, intent(in) :: n
          integer :: i, j
          character(len=20) :: temp_name
          integer :: temp_height

          do i = 1, n - 1
              do j = i + 1, n
                  if (heights(i) < heights(j)) then
                      temp_name = names(i)
                      names(i) = names(j)
                      names(j) = temp_name

                      temp_height = heights(i)
                      heights(i) = heights(j)
                      heights(j) = temp_height
                  end if
              end do
          end do
      end subroutine sort_by_height_aux
      end program sort_by_height
🌐 Data from online sources
def sort_names_by_height(names, heights):
    name_height_pairs = sorted(zip(heights, names), reverse=True)
    sorted_names = [pair[1] for pair in name_height_pairs]
    return sorted_names

The algorithm starts by iterating through both input arrays (names and heights) to create an array ('name_height_pairs') of pairs (in other languages, objects with height and name properties) containing the heights and corresponding names.

Next, the array of pairs is sorted in descending order (highest to lowest) based on the height values.

Finally, a new array (sorted_names) is created containing only the names from the sorted pairs.

This process ensures that the names in the final array are sorted in descending order by the people's heights.

🌐 Data from online sources
#include <vector>
#include <string>
#include <algorithm>

std::vector<std::string> sortNamesByHeight(std::vector<std::string>& names, std::vector<int>& heights) {
    std::vector<std::pair<int, std::string>> name_height_pairs;

    for(int i = 0; i < names.size(); i++) {
        name_height_pairs.push_back(std::make_pair(heights[i], names[i]));
    }

    std::sort(name_height_pairs.rbegin(), name_height_pairs.rend());

    std::vector<std::string> sorted_names;
    for(auto& pair : name_height_pairs) {
        sorted_names.push_back(pair.second);
    }

    return sorted_names;
}

The algorithm starts by iterating through both input arrays (names and heights) to create an array ('name_height_pairs') of pairs (in other languages, objects with height and name properties) containing the heights and corresponding names.

Next, the array of pairs is sorted in descending order (highest to lowest) based on the height values.

Finally, a new array (sorted_names) is created containing only the names from the sorted pairs.

This process ensures that the names in the final array are sorted in descending order by the people's heights.