{"id":6562,"date":"2020-04-04T19:43:31","date_gmt":"2020-04-05T02:43:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6562"},"modified":"2020-04-04T19:45:23","modified_gmt":"2020-04-05T02:45:23","slug":"leetcode-1399-count-largest-group","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1399-count-largest-group\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1399. Count Largest Group"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>.&nbsp;Each number from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>&nbsp;is grouped according to the sum of its digits.&nbsp;<\/p>\n\n\n\n<p>Return&nbsp;how many groups have the largest size.<\/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> n = 13\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13:\n[1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9]. There are 4 groups with largest size.\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> n = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 2 groups [1], [2] of size 1.\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> n = 15\n<strong>Output:<\/strong> 6\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 24\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(logn)<\/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 countLargestGroup(int n) {\n    vector<int> c(37); \/\/ max sum is 9+9+9+9 = 36\n    for (int i = 1; i <= n; ++i) {\n      int x = i;\n      int sum = 0;\n      while (x) {\n        sum += x % 10;\n        x \/= 10;\n      }\n      ++c[sum];\n    }    \n    return count(begin(c), end(c), *max_element(begin(c), end(c)));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n.&nbsp;Each number from&nbsp;1&nbsp;to&nbsp;n&nbsp;is grouped according to the sum of its digits.&nbsp; Return&nbsp;how many groups have the largest size. Example 1: Input: n =&#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":[222,82],"class_list":["post-6562","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6562","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=6562"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6562\/revisions"}],"predecessor-version":[{"id":6564,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6562\/revisions\/6564"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}