<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 {
public:
    std::vector<std::string> sortPeople(std::vector<std::string>& names, std::vector<int>& heights) {
        mergeSort(names, heights);
        return names;
    }

private:
    void mergeSort(std::vector<std::string>& names, std::vector<int>& heights) {

        if (names.size()<=1) return;

        std::vector<std::string> tmp_names(names.begin(), names.end());
        std::vector<int> tmp_heights(heights.begin(), heights.end());
        merge(names, tmp_names, heights, tmp_heights, 0, names.size()-1);
    }

    /*
        heights, names:
        => original vector, use them to check if need to swap

        tmp_heights, tmp_names:
        => vector with same size used when merging subvectors to original vectors
    */

    void merge(std::vector<std::string>& names,
               std::vector<std::string>& tmp_names,
               std::vector<int>& heights,
               std::vector<int>& tmp_heights,
               int l,
               int r) {
        
        // std::cout << "#### merge(): [" << l << "," << r << "]\n";
        // base cases
        if (r==l) return;
        if (r-l==1) {
            if (heights[l]>heights[r]) return; // all heights are distinct
            swap(names, l, r);
            swap(heights, l, r);
        }

        int mid = l+(r-l)/2;
        // non-base cases
        merge(names, tmp_names, heights, tmp_heights, l, mid);
        merge(names, tmp_names, heights, tmp_heights, mid+1, r);


        // copy sorted subvectors into temp
        // so that we can put combined sorted full range to original vectors
        // std::cout << "#### before copy: [" << l << "," << r << "]\n";
        std::copy(names.begin()+l, names.begin()+r+1, tmp_names.begin()+l);
        std::copy(heights.begin()+l, heights.begin()+r+1, tmp_heights.begin()+l);

        // std::cout << "before merging subvectors\n";
        int sub_lp = l;
        int sub_rp = mid+1;
        for (int i=l; i<=r; i++) {
            // std::cout << ">>> i: " << i << "\n";
            // std::cout << "sub_lp: " << sub_lp << "; sub_rp: " << sub_rp << "\n";
            if (sub_lp<=mid && sub_rp<=r) { // L, R subvector both have remaining elems
                if (tmp_heights[sub_lp]>=tmp_heights[sub_rp]) { // use sub_lp
                    names[i] = tmp_names[sub_lp];
                    heights[i] = tmp_heights[sub_lp];
                    sub_lp++;
                } else {
                    names[i] = tmp_names[sub_rp];
                    heights[i] = tmp_heights[sub_rp];
                    sub_rp++;
                }
            } else if (sub_lp<=mid) { // only L subvector have remaining elems
                names[i] = tmp_names[sub_lp];
                heights[i] = tmp_heights[sub_lp];
                sub_lp++;
            } else { // only R subvector have remaining elems
                names[i] = tmp_names[sub_rp];
                heights[i] = tmp_heights[sub_rp];
                sub_rp++;
            }
        }
    }

    template <class T>
    void swap(std::vector<T>& arr, int i, int j) {
        T tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
};
30821