{"id":6138,"date":"2020-01-26T10:01:21","date_gmt":"2020-01-26T18:01:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6138"},"modified":"2020-01-26T10:01:50","modified_gmt":"2020-01-26T18:01:50","slug":"leetcode-1331-rank-transform-of-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1331-rank-transform-of-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1331. Rank Transform of an Array"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>, replace each element with its rank.<\/p>\n\n\n\n<p>The rank represents how large the element is. The rank has the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Rank is an integer starting from 1.<\/li><li>The larger the element, the larger the rank. If two elements are equal, their rank must be the same.<\/li><li>Rank should be as small as possible.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [40,10,20,30]\n<strong>Output:<\/strong> [4,1,2,3]\n<strong>Explanation<\/strong>: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.<\/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> arr = [100,100,100]\n<strong>Output:<\/strong> [1,1,1]\n<strong>Explanation<\/strong>: Same elements share the same rank.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [37,12,28,9,100,56,80,5,12]\n<strong>Output:<\/strong> [5,3,4,2,8,6,7,1,3]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= arr.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= arr[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting + HashTable<\/strong><\/h2>\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<int> arrayRankTransform(vector<int>& arr) {\n    set<int> s(begin(arr), end(arr));\n    unordered_map<int, int> m;\n    int r = 0;\n    for (const int v : s) m[v] = ++r;\n    vector<int> ans(arr.size());\n    for (int i = 0; i < arr.size(); ++i)\n      ans[i] = m[arr[i]];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr, replace each element with its rank. The rank represents how large the element is. The rank has the following rules:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,222,82,376,15],"class_list":["post-6138","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-easy","tag-hashtable","tag-on","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6138","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=6138"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6138\/revisions"}],"predecessor-version":[{"id":6140,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6138\/revisions\/6140"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}