{"id":7579,"date":"2020-10-31T13:20:09","date_gmt":"2020-10-31T20:20:09","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7579"},"modified":"2020-10-31T13:20:45","modified_gmt":"2020-10-31T20:20:45","slug":"leetcode-1636-sort-array-by-increasing-frequency","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1636-sort-array-by-increasing-frequency\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1636. Sort Array by Increasing Frequency"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>nums<\/code>, sort the array in&nbsp;<strong>increasing<\/strong>&nbsp;order based on the frequency of the values. If multiple values have the same frequency, sort them in&nbsp;<strong>decreasing<\/strong>&nbsp;order.<\/p>\n\n\n\n<p>Return the&nbsp;<em>sorted array<\/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> nums = [1,1,2,2,2,3]\n<strong>Output:<\/strong> [3,1,1,2,2,2]\n<strong>Explanation:<\/strong> '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.\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> nums = [2,3,1,3,2]\n<strong>Output:<\/strong> [1,3,3,2,2]\n<strong>Explanation:<\/strong> '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.\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> nums = [-1,1,-6,4,5,-6,1,4,1]\n<strong>Output:<\/strong> [5,-1,4,4,-6,-6,1,1,1]<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>-100 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable + Sorting<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to track the frequency of each number.<\/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++\">\nclass Solution {\npublic:\n  vector<int> frequencySort(vector<int>& nums) {\n    unordered_map<int, int> freq;\n    for (int x : nums) ++freq[x];\n    sort(begin(nums), end(nums), [&](int a, int b) {\n      if (freq[a] != freq[b]) return freq[a] < freq[b];\n      return a > b;\n    });\n    return nums;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums, sort the array in&nbsp;increasing&nbsp;order based on the frequency of the values. If multiple values have the same frequency, sort them&#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],"class_list":["post-7579","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7579","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=7579"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7579\/revisions"}],"predecessor-version":[{"id":7581,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7579\/revisions\/7581"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}