{"id":2031,"date":"2018-03-08T00:37:29","date_gmt":"2018-03-08T08:37:29","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2031"},"modified":"2018-03-08T00:48:58","modified_gmt":"2018-03-08T08:48:58","slug":"leetcode-506-relative-ranks","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-506-relative-ranks\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 506. Relative Ranks"},"content":{"rendered":"<div class=\"question-description\">\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u4eba\u7684\u5206\u6570\uff0c\u8ba9\u4f60\u8f93\u51fa\u4ed6\u4eec\u7684\u6392\u540d\uff0c\u524d\u4e09\u540d\u8f93\u51fa\u91d1\u94f6\u94dc\u724c\u3002<\/p>\n<p><strong>Problem:\u00a0<\/strong><\/p>\n<p>Given scores of\u00a0<b>N<\/b>\u00a0athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: &#8220;Gold Medal&#8221;, &#8220;Silver Medal&#8221; and &#8220;Bronze Medal&#8221;.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: [5, 4, 3, 2, 1]\r\nOutput: [\"Gold Medal\", \"Silver Medal\", \"Bronze Medal\", \"4\", \"5\"]\r\nExplanation: The first three athletes got the top three highest scores, so they got \"Gold Medal\", \"Silver Medal\" and \"Bronze Medal\". \r\nFor the left two athletes, you just need to output their relative ranks according to their scores.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>N is a positive integer and won&#8217;t exceed 10,000.<\/li>\n<li>All the scores of athletes are guaranteed to be unique.<\/li>\n<\/ol>\n<\/div>\n<p><strong>Solution 1: Sorting ( + Binary Search)<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  vector&lt;string&gt; findRelativeRanks(vector&lt;int&gt;&amp; nums) {\r\n    vector&lt;string&gt; ans;\r\n    vector&lt;int&gt; s(nums);\r\n    std::sort(s.begin(), s.end());\r\n    vector&lt;string&gt; medals{\"Gold\", \"Silver\", \"Bronze\"};\r\n    for (int i = 0; i &lt; nums.size(); ++i) {\r\n      int rank = s.end() - std::lower_bound(s.begin(), s.end(), nums[i]);\r\n      if (rank &lt;= 3) \r\n        ans.push_back(medals[rank - 1] + \" Medal\");\r\n      else \r\n        ans.push_back(std::to_string(rank));\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 13 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; findRelativeRanks(vector&lt;int&gt;&amp; nums) {    \r\n    vector&lt;pair&lt;int, int&gt;&gt; s;\r\n    for (int i = 0; i &lt; nums.size(); ++i)\r\n      s.emplace_back(nums[i], i);    \r\n    vector&lt;string&gt; medals{\"Gold\", \"Silver\", \"Bronze\"};\r\n    std::sort(s.rbegin(), s.rend());    \r\n    vector&lt;string&gt; ans(nums.size());\r\n    for (int i = 0; i &lt; s.size(); ++i)\r\n      ans[s[i].second] = i &lt; 3 ? (medals[i] + \" Medal\") : std::to_string(i + 1);    \r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u4eba\u7684\u5206\u6570\uff0c\u8ba9\u4f60\u8f93\u51fa\u4ed6\u4eec\u7684\u6392\u540d\uff0c\u524d\u4e09\u540d\u8f93\u51fa\u91d1\u94f6\u94dc\u724c\u3002 Problem:\u00a0 Given scores of\u00a0N\u00a0athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: &#8220;Gold Medal&#8221;,&#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,149],"tags":[52,15],"class_list":["post-2031","post","type-post","status-publish","format-standard","hentry","category-array","category-binary-search","tag-binary-search","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2031","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=2031"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2031\/revisions"}],"predecessor-version":[{"id":2036,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2031\/revisions\/2036"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}