{"id":6932,"date":"2020-06-14T19:40:03","date_gmt":"2020-06-15T02:40:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6932"},"modified":"2020-06-14T19:42:25","modified_gmt":"2020-06-15T02:42:25","slug":"leetcode-1481-least-number-of-unique-integers-after-k-removals","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1481-least-number-of-unique-integers-after-k-removals\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1481. Least Number of Unique Integers after K Removals"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.&nbsp;Find the&nbsp;<em>least number of unique integers<\/em>&nbsp;after removing&nbsp;<strong>exactly<\/strong>&nbsp;<code>k<\/code>&nbsp;elements<strong>.<\/strong><\/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>arr = [5,5,4], k = 1\n<strong>Output: <\/strong>1\n<strong>Explanation<\/strong>: Remove the single 4, only 5 is left.\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>arr = [4,3,1,1,3,3,2], k = 3\n<strong>Output: <\/strong>2\n<strong>Explanation<\/strong>: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length&nbsp;&lt;= 10^5<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10^9<\/code><\/li><li><code>0 &lt;= k&nbsp;&lt;= arr.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Count the frequency of each unique number. Sort by frequency, remove items with lowest frequency first.<\/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  int findLeastNumOfUniqueInts(vector<int>& arr, int k) {    \n    unordered_map<int, int> c;\n    for (int x : arr) ++c[x];\n    vector<int> m; \/\/ freq\n    for (const auto [x, f] : c)\n      m.push_back(f);\n    sort(begin(m), end(m));\n    int ans = m.size();    \n    int i = 0;\n    while (k--) {\n      if (--m[i] == 0) {\n        ++i;\n        --ans;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr&nbsp;and an integer&nbsp;k.&nbsp;Find the&nbsp;least number of unique integers&nbsp;after removing&nbsp;exactly&nbsp;k&nbsp;elements. Example 1: Input: arr = [5,5,4], k = 1 Output: 1 Explanation:&#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":[24,88,82,177,23],"class_list":["post-6932","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-frequency","tag-greedy","tag-hashtable","tag-medium","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6932","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=6932"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6932\/revisions"}],"predecessor-version":[{"id":6934,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6932\/revisions\/6934"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}