<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>guessing Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/guessing/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/guessing/</link>
	<description></description>
	<lastBuildDate>Mon, 15 Jun 2020 03:02:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>guessing Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/guessing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1482. Minimum Number of Days to Make m Bouquets</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1482-minimum-number-of-days-to-make-m-bouquets/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1482-minimum-number-of-days-to-make-m-bouquets/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 15 Jun 2020 02:53:00 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[guessing]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6935</guid>

					<description><![CDATA[<p>Given an integer array&#160;bloomDay, an integer&#160;m&#160;and an integer&#160;k. We need to make&#160;m&#160;bouquets. To make a bouquet,&#160;you need to use&#160;k&#160;adjacent flowers&#160;from the garden. The garden consists&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1482-minimum-number-of-days-to-make-m-bouquets/">花花酱 LeetCode 1482. Minimum Number of Days to Make m Bouquets</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an integer array&nbsp;<code>bloomDay</code>, an integer&nbsp;<code>m</code>&nbsp;and an integer&nbsp;<code>k</code>.</p>



<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>



<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>



<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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 1
<strong>Output:</strong> 3
<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.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _]   // we can only make one bouquet.
After day 2: [x, _, _, _, x]   // we can only make two bouquets.
After day 3: [x, _, x, _, x]   // we can make 3 bouquets. The answer is 3.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> bloomDay = [1,10,3,10,2], m = 3, k = 2
<strong>Output:</strong> -1
<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.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
<strong>Output:</strong> 12
<strong>Explanation:</strong> We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We 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.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted:crayon:false"><strong>Input:</strong> bloomDay = [1000000000,1000000000], m = 1, k = 1
<strong>Output:</strong> 1000000000
<strong>Explanation:</strong> You need to wait 1000000000 days to have a flower ready for a bouquet.
</pre>



<p><strong>Example 5:</strong></p>



<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
<strong>Output:</strong> 9
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Binary Search</strong></h2>



<p>Find the smallest day D that we can make <strong>at least m</strong> bouquets using binary search.</p>



<p>at a given day, we can check how many bouquets we can make in O(n)</p>



<p>Time complexity: O(nlog(max(days))<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minDays(vector&lt;int&gt;&amp; bloomDay, int m, int k) {
    const auto [lo, hi] = minmax_element(begin(bloomDay), end(bloomDay));
    const int kInf = *hi + 1;
    int l = *lo;
    int r = kInf;
    
    // Return the number of bouquets we can get at day D.
    auto getBouquets = [&amp;](int D) {
      int ans = 0;
      int cur = 0;
      for (int d : bloomDay) {
        if (d &gt; D) {
          cur = 0;
        } else if (++cur == k) {
          ++ans;
          cur = 0;          
        }
      }
      return ans;
    };
          
    while (l &lt; r) {
      int mid = l + (r - l) / 2;      
      // Find smallest day that bouquets &gt;= m.
      if (getBouquets(mid) &gt;= m)
        r = mid;
      else
        l = mid + 1;
    }
    return l &gt;= kInf ? -1 : l;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1482-minimum-number-of-days-to-make-m-bouquets/">花花酱 LeetCode 1482. Minimum Number of Days to Make m Bouquets</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-1482-minimum-number-of-days-to-make-m-bouquets/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
