{"id":6935,"date":"2020-06-14T19:53:00","date_gmt":"2020-06-15T02:53:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6935"},"modified":"2020-06-14T20:02:03","modified_gmt":"2020-06-15T03:02:03","slug":"leetcode-1482-minimum-number-of-days-to-make-m-bouquets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1482-minimum-number-of-days-to-make-m-bouquets\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1482. Minimum Number of Days to Make m Bouquets"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>bloomDay<\/code>, an integer&nbsp;<code>m<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>We need to make&nbsp;<code>m<\/code>&nbsp;bouquets. To make a bouquet,&nbsp;you need to use&nbsp;<code>k<\/code>&nbsp;<strong>adjacent flowers<\/strong>&nbsp;from the garden.<\/p>\n\n\n\n<p>The garden consists of&nbsp;<code>n<\/code>&nbsp;flowers, the&nbsp;<code>ith<\/code>&nbsp;flower will bloom in the&nbsp;<code>bloomDay[i]<\/code>&nbsp;and then can be used in&nbsp;<strong>exactly one<\/strong>&nbsp;bouquet.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of days<\/em>&nbsp;you need to wait to be able to make&nbsp;<code>m<\/code>&nbsp;bouquets from the garden. If it is impossible to make&nbsp;<code>m<\/code>&nbsp;bouquets return&nbsp;<strong>-1<\/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> bloomDay = [1,10,3,10,2], m = 3, k = 1\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.\nWe need 3 bouquets each should contain 1 flower.\nAfter day 1: [x, _, _, _, _]   \/\/ we can only make one bouquet.\nAfter day 2: [x, _, _, _, x]   \/\/ we can only make two bouquets.\nAfter day 3: [x, _, x, _, x]   \/\/ we can make 3 bouquets. The answer is 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> bloomDay = [1,10,3,10,2], m = 3, k = 2\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -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> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong> We need 2 bouquets each should have 3 flowers.\nHere's the garden after the 7 and 12 days:\nAfter day 7: [x, x, x, x, _, x, x]\nWe can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.\nAfter day 12: [x, x, x, x, x, x, x]\nIt is obvious that we can make two bouquets in different ways.\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> bloomDay = [1000000000,1000000000], m = 1, k = 1\n<strong>Output:<\/strong> 1000000000\n<strong>Explanation:<\/strong> You need to wait 1000000000 days to have a flower ready for a bouquet.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted:crayon:false\"><strong>Input:<\/strong> bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2\n<strong>Output:<\/strong> 9\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>bloomDay.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10^5<\/code><\/li><li><code>1 &lt;= bloomDay[i] &lt;= 10^9<\/code><\/li><li><code>1 &lt;= m &lt;= 10^6<\/code><\/li><li><code>1 &lt;= k &lt;= n<\/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 day D that we can make <strong>at least m<\/strong> bouquets using binary search.<\/p>\n\n\n\n<p>at a given day, we can check how many bouquets we can make in O(n)<\/p>\n\n\n\n<p>Time complexity: O(nlog(max(days))<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 minDays(vector<int>& bloomDay, int m, int k) {\n    const auto [lo, hi] = minmax_element(begin(bloomDay), end(bloomDay));\n    const int kInf = *hi + 1;\n    int l = *lo;\n    int r = kInf;\n    \n    \/\/ Return the number of bouquets we can get at day D.\n    auto getBouquets = [&](int D) {\n      int ans = 0;\n      int cur = 0;\n      for (int d : bloomDay) {\n        if (d > D) {\n          cur = 0;\n        } else if (++cur == k) {\n          ++ans;\n          cur = 0;          \n        }\n      }\n      return ans;\n    };\n          \n    while (l < r) {\n      int mid = l + (r - l) \/ 2;      \n      \/\/ Find smallest day that bouquets >= m.\n      if (getBouquets(mid) >= m)\n        r = mid;\n      else\n        l = mid + 1;\n    }\n    return l >= kInf ? -1 : l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;bloomDay, an integer&nbsp;m&nbsp;and an integer&nbsp;k. We need to make&nbsp;m&nbsp;bouquets. To make a bouquet,&nbsp;you need to use&nbsp;k&nbsp;adjacent flowers&nbsp;from the garden. The garden consists&#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,619,177],"class_list":["post-6935","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-guessing","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6935","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=6935"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6935\/revisions"}],"predecessor-version":[{"id":6939,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6935\/revisions\/6939"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}