{"id":8108,"date":"2021-02-13T21:49:03","date_gmt":"2021-02-14T05:49:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8108"},"modified":"2021-02-20T15:06:15","modified_gmt":"2021-02-20T23:06:15","slug":"leetcode-1760-minimum-limit-of-balls-in-a-bag","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1760-minimum-limit-of-balls-in-a-bag\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1760. Minimum Limit of Balls in a Bag"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1760. Minimum Limit of Balls in a Bag - \u5237\u9898\u627e\u5de5\u4f5c EP384\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/yEHgt1UZo7A?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given an integer array\u00a0<code>nums<\/code>\u00a0where the\u00a0<code>i<sup>th<\/sup><\/code>\u00a0bag contains\u00a0<code>nums[i]<\/code>\u00a0balls. You are also given an integer\u00a0<code>maxOperations<\/code>.<\/p>\n\n\n\n<p>You can perform the following operation at most&nbsp;<code>maxOperations<\/code>&nbsp;times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Take any bag of balls and divide it into two new bags with a&nbsp;<strong>positive&nbsp;<\/strong>number of balls.<ul><li>For example, a bag of&nbsp;<code>5<\/code>&nbsp;balls can become two new bags of&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>4<\/code>&nbsp;balls, or two new bags of&nbsp;<code>2<\/code>&nbsp;and&nbsp;<code>3<\/code>&nbsp;balls.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Your penalty is the&nbsp;<strong>maximum<\/strong>&nbsp;number of balls in a bag. You want to&nbsp;<strong>minimize<\/strong>&nbsp;your penalty after the operations.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum possible penalty&nbsp;after performing the operations<\/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 = [9], maxOperations = 2\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \n- Divide the bag with 9 balls into two bags of sizes 6 and 3. [<strong><u>9<\/u><\/strong>] -&gt; [6,3].\n- Divide the bag with 6 balls into two bags of sizes 3 and 3. [<strong><u>6<\/u><\/strong>,3] -&gt; [3,3,3].\nThe bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 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,4,8,2], maxOperations = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\n- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,<strong><u>8<\/u><\/strong>,2] -&gt; [2,4,4,4,2].\n- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,<strong><u>4<\/u><\/strong>,4,4,2] -&gt; [2,2,2,4,4,2].\n- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,<strong><u>4<\/u><\/strong>,4,2] -&gt; [2,2,2,2,2,4,2].\n- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,<strong><u>4<\/u><\/strong>,2] -&gt; [2,2,2,2,2,2,2,2].\nThe bag with the most number of balls has 2 balls, so your penalty is 2 an you should return 2.\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 = [7,17], maxOperations = 2\n<strong>Output:<\/strong> 7\n<\/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;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= maxOperations, nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384.png\" alt=\"\" class=\"wp-image-8122\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-1.png\" alt=\"\" class=\"wp-image-8123\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-2.png\" alt=\"\" class=\"wp-image-8125\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1760-ep384-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Find the smallest penalty that requires less or equal ops than max_ops.<\/p>\n\n\n\n<p>Time complexity: O(nlogm)<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 minimumSize(vector<int>& nums, int maxOperations) {\n    int l = 1, r = *max_element(begin(nums), end(nums));\n    while (l < r) {\n      const int m = l + (r - l) \/ 2;\n      int count = 0;\n      for (int x : nums) \n        count += (x - 1) \/ m;\n      if (count <= maxOperations)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array\u00a0nums\u00a0where the\u00a0ith\u00a0bag contains\u00a0nums[i]\u00a0balls. You are also given an integer\u00a0maxOperations. You can perform the following operation at most&nbsp;maxOperations&nbsp;times: Take any bag&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177],"class_list":["post-8108","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8108","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=8108"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8108\/revisions"}],"predecessor-version":[{"id":8126,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8108\/revisions\/8126"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}