{"id":9842,"date":"2022-09-26T09:57:17","date_gmt":"2022-09-26T16:57:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9842"},"modified":"2022-09-26T09:57:47","modified_gmt":"2022-09-26T16:57:47","slug":"leetcode-2418-sort-the-people","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2418-sort-the-people\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2418.\u00a0Sort the People"},"content":{"rendered":"\n<p>You are given an array of strings&nbsp;<code>names<\/code>, and an array&nbsp;<code>heights<\/code>&nbsp;that consists of&nbsp;<strong>distinct<\/strong>&nbsp;positive integers. Both arrays are of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>For each index&nbsp;<code>i<\/code>,&nbsp;<code>names[i]<\/code>&nbsp;and&nbsp;<code>heights[i]<\/code>&nbsp;denote the name and height of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;person.<\/p>\n\n\n\n<p>Return&nbsp;<code>names<\/code><em>&nbsp;sorted in&nbsp;<strong>descending<\/strong>&nbsp;order by the people&#8217;s heights<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> names = [\"Mary\",\"John\",\"Emma\"], heights = [180,165,170]\n<strong>Output:<\/strong> [\"Mary\",\"Emma\",\"John\"]\n<strong>Explanation:<\/strong> Mary is the tallest, followed by Emma and John.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> names = [\"Alice\",\"Bob\",\"Bob\"], heights = [155,185,150]\n<strong>Output:<\/strong> [\"Bob\",\"Alice\",\"Bob\"]\n<strong>Explanation:<\/strong> The first Bob is the tallest, followed by Alice and the second Bob.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>&nbsp;consists of lower and upper case English letters.<\/li><li>All the values of&nbsp;<code>heights<\/code>&nbsp;are distinct.<\/li><\/ul>\n\n\n\n<p>Solution: Zip and sort<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> sortPeople(vector<string>& names, vector<int>& heights) {\n    vector<pair<int, string*>> data;\n    for (int i = 0; i < names.size(); ++i)\n      data.emplace_back(heights[i], &#038;names[i]);    \n    sort(rbegin(data), rend(data));\n    vector<string> ans(names.size());\n    for (int i = 0; i < names.size(); ++i)\n      ans[i].swap(*data[i].second);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of strings&nbsp;names, and an array&nbsp;heights&nbsp;that consists of&nbsp;distinct&nbsp;positive integers. Both arrays are of length&nbsp;n. For each index&nbsp;i,&nbsp;names[i]&nbsp;and&nbsp;heights[i]&nbsp;denote the name and height&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,23,785],"class_list":["post-9842","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-sort","tag-zip","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9842"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9842\/revisions"}],"predecessor-version":[{"id":9844,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9842\/revisions\/9844"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}