{"id":8045,"date":"2021-01-30T20:31:10","date_gmt":"2021-01-31T04:31:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8045"},"modified":"2021-01-30T20:31:53","modified_gmt":"2021-01-31T04:31:53","slug":"leetcode-1742-maximum-number-of-balls-in-a-box","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1742-maximum-number-of-balls-in-a-box\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1742. Maximum Number of Balls in a Box"},"content":{"rendered":"\n<p>You are working in a ball factory where you have&nbsp;<code>n<\/code>&nbsp;balls numbered from&nbsp;<code>lowLimit<\/code>&nbsp;up to&nbsp;<code>highLimit<\/code>&nbsp;<strong>inclusive<\/strong>&nbsp;(i.e.,&nbsp;<code>n == highLimit - lowLimit + 1<\/code>), and an infinite number of boxes numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>infinity<\/code>.<\/p>\n\n\n\n<p>Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball&#8217;s number. For example, the ball number&nbsp;<code>321<\/code>&nbsp;will be put in the box number&nbsp;<code>3 + 2 + 1 = 6<\/code>&nbsp;and the ball number&nbsp;<code>10<\/code>&nbsp;will be put in the box number&nbsp;<code>1 + 0 = 1<\/code>.<\/p>\n\n\n\n<p>Given two integers&nbsp;<code>lowLimit<\/code>&nbsp;and&nbsp;<code>highLimit<\/code>, return<em>&nbsp;the number of balls in the box with the most balls.<\/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> lowLimit = 1, highLimit = 10\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nBox Number:  1 2 3 4 5 6 7 8 9 10 11 ...\nBall Count:  2 1 1 1 1 1 1 1 1 0  0  ...\nBox 1 has the most number of balls with 2 balls.<\/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> lowLimit = 5, highLimit = 15\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nBox Number:  1 2 3 4 5 6 7 8 9 10 11 ...\nBall Count:  1 1 1 1 2 2 1 1 1 0  0  ...\nBoxes 5 and 6 have the most number of balls with 2 balls in each.\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> lowLimit = 19, highLimit = 28\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nBox Number:  1 2 3 4 5 6 7 8 9 10 11 12 ...\nBall Count:  0 1 1 1 1 1 1 1 1 2  0  0  ...\nBox 10 has the most number of balls with 2 balls.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= lowLimit &lt;= highLimit &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable and base-10<\/strong><\/h2>\n\n\n\n<p>Max sum will be 9+9+9+9+9 = 45<\/p>\n\n\n\n<p>Time complexity: O((hi-lo) * log(hi))<br>Space complexity: O(1)<\/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 countBalls(int lowLimit, int highLimit) {\n    vector<int> balls(46);\n    int ans = 0;\n    for (int i = lowLimit; i <= highLimit; ++i) {\n      int n = i;\n      int box = 0;\n      while (n) { box += n % 10; n \/= 10; }\n      ans = max(ans, ++balls[box]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n# Author: Huahua\nclass Solution:\n  def countBalls(self, lowLimit: int, highLimit: int) -> int:\n    balls = defaultdict(int)\n    ans = 0\n    for x in range(lowLimit, highLimit + 1):\n      s = sum(int(d) for d in str(x))\n      balls[s] += 1\n      ans = max(ans, balls[s])\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are working in a ball factory where you have&nbsp;n&nbsp;balls numbered from&nbsp;lowLimit&nbsp;up to&nbsp;highLimit&nbsp;inclusive&nbsp;(i.e.,&nbsp;n == highLimit &#8211; lowLimit + 1), and an infinite number of boxes&#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":[695,93,82,4],"class_list":["post-8045","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-base10","tag-conversion","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8045","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=8045"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8045\/revisions"}],"predecessor-version":[{"id":8048,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8045\/revisions\/8048"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}