<p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>

<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>i<sup>th</sup></code> person.</p>

<p>Return <code>names</code><em> sorted in <strong>descending</strong> order by the people&#39;s heights</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]
<strong>Output:</strong> [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]
<strong>Explanation:</strong> Mary is the tallest, followed by Emma and John.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> names = [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights = [155,185,150]
<strong>Output:</strong> [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]
<strong>Explanation:</strong> The first Bob is the tallest, followed by Alice and the second Bob.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == names.length == heights.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
	<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
	<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>names[i]</code> consists of lower and upper case English letters.</li>
	<li>All the values of <code>heights</code> are distinct.</li>
</ul>

class Solution:
    def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
        sorted_names, sorted_heights = self.merge_sort(names, heights)
        return sorted_names

    def merge_sort(self, names: list[str], heights: list[str]):
        length = len(names)

        if length <= 1:
            return names, heights
        
        mid = length // 2
        left_names = names[:mid]
        left_heights = heights[:mid]

        right_names = names[mid:]
        right_heights = heights[mid:]

        n1, h1 = self.merge_sort(left_names, left_heights)
        n2, h2 = self.merge_sort(right_names, right_heights)

        return self.merge(n1, n2, h1, h2)

    def merge(self, n1: list[str], n2: list[str], h1: list[int], h2: list[int]):
        h1_ptr = 0
        h2_ptr = 0

        res_h = []
        res_n = []

        while h1_ptr < len(h1) and h2_ptr < len(h2):
            if h1[h1_ptr] < h2[h2_ptr]:
                res_h.append(h2[h2_ptr])
                res_n.append(n2[h2_ptr])

                h2_ptr += 1
            else:
                res_h.append(h1[h1_ptr])
                res_n.append(n1[h1_ptr])

                h1_ptr += 1

        if h1_ptr < len(h1):
            res_h.extend(h1[h1_ptr:])
            res_n.extend(n1[h1_ptr:])
        else:
            res_h.extend(h2[h2_ptr:])
            res_n.extend(n2[h2_ptr:])

        return res_n, res_h


139