{"id":8682,"date":"2021-11-07T08:48:05","date_gmt":"2021-11-07T16:48:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8682"},"modified":"2021-11-07T08:50:52","modified_gmt":"2021-11-07T16:50:52","slug":"leetcode-2064-minimized-maximum-of-products-distributed-to-any-store","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-2064-minimized-maximum-of-products-distributed-to-any-store\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2064. Minimized Maximum of Products Distributed to Any Store"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>n<\/code>&nbsp;indicating there are&nbsp;<code>n<\/code>&nbsp;specialty retail stores. There are&nbsp;<code>m<\/code>&nbsp;product types of varying amounts, which are given as a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>quantities<\/code>, where&nbsp;<code>quantities[i]<\/code>&nbsp;represents the number of products of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;product type.<\/p>\n\n\n\n<p>You need to distribute&nbsp;<strong>all products<\/strong>&nbsp;to the retail stores following these rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A store can only be given&nbsp;<strong>at most one product type<\/strong>&nbsp;but can be given&nbsp;<strong>any<\/strong>&nbsp;amount of it.<\/li><li>After distribution, each store will be given some number of products (possibly&nbsp;<code>0<\/code>). Let&nbsp;<code>x<\/code>&nbsp;represent the maximum number of products given to any store. You want&nbsp;<code>x<\/code>&nbsp;to be as small as possible, i.e., you want to&nbsp;<strong>minimize<\/strong>&nbsp;the&nbsp;<strong>maximum<\/strong>&nbsp;number of products that are given to any store.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the minimum possible<\/em>&nbsp;<code>x<\/code>.<\/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 = 6, quantities = [11,6]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> One optimal way is:\n- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3\n- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3\nThe maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 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> n = 7, quantities = [15,10,10]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> One optimal way is:\n- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5\n- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5\n- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5\nThe maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.\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 = 1, quantities = [100000]\n<strong>Output:<\/strong> 100000\n<strong>Explanation:<\/strong> The only optimal way is:\n- The 100000 products of type 0 are distributed to the only store.\nThe maximum number of products given to any store is max(100000) = 100000.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == quantities.length<\/code><\/li><li><code>1 &lt;= m &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= quantities[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Find the smallest max product s.t. all products can be distribute to &lt;= n stores.<\/p>\n\n\n\n<p>Time complexity: O(nlog(max(q)))<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 minimizedMaximum(int n, vector<int>& quantities) {\n    int l = 1;\n    int r = *max_element(begin(quantities), end(quantities)) + 1;\n    while (l < r) {\n      int cur = 0;\n      int m = l + (r - l) \/ 2;\n      for (int q : quantities)\n        cur += (q + (m - 1)) \/ m;\n      if (cur <= n)\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&nbsp;n&nbsp;indicating there are&nbsp;n&nbsp;specialty retail stores. There are&nbsp;m&nbsp;product types of varying amounts, which are given as a&nbsp;0-indexed&nbsp;integer array&nbsp;quantities, where&nbsp;quantities[i]&nbsp;represents the number of&#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-8682","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\/8682","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=8682"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8682\/revisions"}],"predecessor-version":[{"id":8684,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8682\/revisions\/8684"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}