<?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>bucket Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/bucket/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/bucket/</link>
	<description></description>
	<lastBuildDate>Thu, 30 Aug 2018 20:48:09 +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>bucket Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/bucket/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 164. Maximum Gap</title>
		<link>https://zxi.mytechroad.com/blog/difficulty/hard/leetcode-164-maximum-gap/</link>
					<comments>https://zxi.mytechroad.com/blog/difficulty/hard/leetcode-164-maximum-gap/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 11 Dec 2017 00:19:22 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[bucket]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1173</guid>

					<description><![CDATA[<p>https://leetcode.com/problems/maximum-gap/description/ Problem: Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/difficulty/hard/leetcode-164-maximum-gap/">花花酱 LeetCode 164. Maximum Gap</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><a href="https://leetcode.com/problems/maximum-gap/description/">https://leetcode.com/problems/maximum-gap/description/</a></p>
<p><strong>Problem:</strong></p>
<p>Given an unsorted array, find the maximum difference between the successive elements in its sorted form.</p>
<p>Try to solve it in linear time/space.</p>
<p>Return 0 if the array contains less than 2 elements.</p>
<p>You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.</p>
<p>题目大意:</p>
<p>给你一个没有排序的正整数数组。输出排序后，相邻元素的差的最大值(Max Gap)。需要在线性时间内解决。</p>
<p><strong>Example:</strong></p>
<p><span style="font-weight: 400;">Input:  [5, 0, 4, 2, 12, 10]</span></p>
<p>Output: 5</p>
<p><strong>Explanation: </strong></p>
<p><span style="font-weight: 400;">Sorted: [0, 2, 4, 5, 10, 12]</span></p>
<p>max gap is 10 &#8211; 5 = 5</p>
<p><strong>Idea:</strong></p>
<p>Bucket sort. Use n buckets to store all the numbers. For each bucket, only track the min / max value.</p>
<p>桶排序。用n个桶来存放数字。对于每个桶，只跟踪存储最大值和最小值。</p>
<p>max gap must come from two &#8220;adjacent&#8221; buckets, b[i], b[j], j &gt; i, b[i+1] &#8230; b[j &#8211; 1] must be empty.</p>
<p>max gap 只可能来自&#8221;相邻&#8221;的两个桶 b[i] 和 b[j], j &gt; i, b[i] 和 b[j] 之间的桶（如果有）必须为空。</p>
<p>max gap = b[j].min &#8211; b[i].min</p>
<p>Time complexity: O(n)</p>
<p>时间复杂度: O(n)</p>
<p>Space complexity: O(n)</p>
<p>空间复杂度: O(n)</p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    int maximumGap(vector&lt;int&gt;&amp; nums) {
        const int n = nums.size();
        if (n &lt;= 1) return 0;
        
        const auto mm = minmax_element(nums.begin(), nums.end());
        const int range = *mm.second - *mm.first;
        const int bin_size = range / n + 1;
        vector&lt;int&gt; min_vals(n, INT_MAX);
        vector&lt;int&gt; max_vals(n, INT_MIN);
        for (const int num : nums) {
            const int index = (num - *mm.first) / bin_size;
            min_vals[index] = min(min_vals[index], num);
            max_vals[index] = max(max_vals[index], num);
        }
        
        int max_gap = 0;
        int prev_max = max_vals[0];
        for (int i = 1; i &lt; n; ++i) {
            if (min_vals[i] != INT_MAX) {
                max_gap = max(max_gap, min_vals[i] - prev_max);
                prev_max = max_vals[i];
            }
        }
        return max_gap;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/difficulty/hard/leetcode-164-maximum-gap/">花花酱 LeetCode 164. Maximum Gap</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/difficulty/hard/leetcode-164-maximum-gap/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 719. Find K-th Smallest Pair Distance</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Oct 2017 20:14:10 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[Hard]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[bucket]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=710</guid>

					<description><![CDATA[<p>题目大意：给你一个数组，返回所有数对中，绝对值差第k小的值。 Problem: Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/">花花酱 LeetCode 719. Find K-th Smallest Pair Distance</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><iframe width="500" height="375" src="https://www.youtube.com/embed/WHfljqX61Y8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：给你一个数组，返回所有数对中，绝对值差第k小的值。</p>
<p><strong>Problem:</strong></p>
<p>Given an integer array, return the k-th smallest <b>distance</b> among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input:
nums = [1,3,1]
k = 1
Output: 0 
Explanation:
Here are all the pairs:
(1,3) -&gt; 2
(1,1) -&gt; 0
(3,1) -&gt; 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.</pre><p><b>Note:</b></p>
<ol>
<li><code>2 &lt;= len(nums) &lt;= 10000</code>.</li>
<li><code>0 &lt;= nums[i] &lt; 1000000</code>.</li>
<li><code>1 &lt;= k &lt;= len(nums) * (len(nums) - 1) / 2</code>.</li>
</ol>
<p><strong>Idea</strong></p>
<p>Bucket sort</p>
<p>Binary search / dp</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1.png"><img class="alignnone size-full wp-image-720" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2.png"><img class="alignnone size-full wp-image-719" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/719-ep99-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution</strong></p>
<p>C++ / binary search</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    int smallestDistancePair(vector&lt;int&gt;&amp; nums, int k) {
        std::sort(nums.begin(), nums.end());
        int n = nums.size();
        int l = 0;
        int r = nums.back() - nums.front();
        while (l &lt;= r) {
            int cnt = 0;
            int j = 0;
            int m = l + (r - l) / 2;
            for (int i = 0; i &lt; n; ++i) {
                while (j &lt; n &amp;&amp; nums[j] - nums[i] &lt;= m) ++j;
                cnt += j - i - 1;
            }
            cnt &gt;= k ? r = m - 1 : l = m + 1;
        }        
        return l;
    }
};</pre><p>&nbsp;</p>
<p>C++ / bucket sort w/ vector O(n^2)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 549 ms
class Solution {
public:
    int smallestDistancePair(vector&lt;int&gt;&amp; nums, int k) {
        std::sort(nums.begin(), nums.end());
        const int N = nums.back();
        vector&lt;int&gt; count(N + 1, 0);        
        const int n = nums.size();
        for (int i = 0; i &lt; n; ++i)
            for (int j = i + 1; j &lt; n; ++j)
               ++count[nums[j] - nums[i]];
        for (int i = 0; i &lt;= N; ++i) {
            k -= count[i];
            if (k &lt;= 0) return i;
        }
        return 0;
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/two-pointers/leetcode-786-k-th-smallest-prime-fraction/">花花酱 LeetCode 786. K-th Smallest Prime Fraction</a></li>
<li><a href="http://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-378-kth-smallest-element-in-a-sorted-matrix/">花花酱 LeetCode 378. Kth Smallest Element in a Sorted Matrix</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/">花花酱 LeetCode 719. Find K-th Smallest Pair Distance</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/divide-and-conquer/leetcode-719-find-k-th-smallest-pair-distance/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 683. K Empty Slots</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-683-k-empty-slots/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-683-k-empty-slots/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 30 Sep 2017 05:31:31 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[BST]]></category>
		<category><![CDATA[bucket]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=471</guid>

					<description><![CDATA[<p>题目大意：有n个花盆，第i天，第flowers[i]个花盆的花会开。问是否存在一天，两朵花之间有k个空花盆。 Problem: There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-683-k-empty-slots/">花花酱 LeetCode 683. K Empty Slots</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><iframe width="500" height="375" src="https://www.youtube.com/embed/K8Nk0AiIX4s?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：有n个花盆，第i天，第flowers[i]个花盆的花会开。问是否存在一天，两朵花之间有k个空花盆。</p>
<p><strong>Problem:</strong></p>
<p>There is a garden with <code>N</code> slots. In each slot, there is a flower. The <code>N</code> flowers will bloom one by one in <code>N</code> days. In each day, there will be <code>exactly</code> one flower blooming and it will be in the status of blooming since then.</p>
<p>Given an array <code>flowers</code> consists of number from <code>1</code> to <code>N</code>. Each number in the array represents the place where the flower will open in that day.</p>
<p>For example, <code>flowers[i] = x</code> means that the unique flower that blooms at day <code>i</code> will be at position <code>x</code>, where <code>i</code> and <code>x</code> will be in the range from <code>1</code> to <code>N</code>.</p>
<p>Also given an integer <code>k</code>, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is <code>k</code> and these flowers are not blooming.</p>
<p>If there isn&#8217;t such day, output -1.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false">Input: 
flowers: [1,3,2]
k: 1
Output: 2
Explanation: In the second day, the first and the third flower have become blooming.
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false">Input: 
flowers: [1,2,3]
k: 1
Output: -1
</pre>
<p><b>Note:</b></p>
<ol>
<li>The given array will be in the range [1, 20000].</li>
</ol>
<p><strong>Idea:</strong></p>
<p>BST/Buckets</p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>&nbsp;</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76.png"><img class="alignnone size-full wp-image-481" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-2.png"><img class="alignnone size-full wp-image-480" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-3.png"><img class="alignnone size-full wp-image-479" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-3-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/683-ep76-3-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<h1><strong>Solution 2: BST</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 228 ms
class Solution {
public:
    int kEmptySlots(vector&lt;int&gt;&amp; flowers, int k) {
        int n = flowers.size();
        if (n == 0 || k &gt;= n) return -1;        
        set&lt;int&gt; xs;        
        for (int i = 0; i &lt; n; ++i) {
            int x = flowers[i];
            auto r = xs.insert(x).first;
            auto l = r;
            if (++r != xs.end() &amp;&amp; *r == x + k + 1) return i + 1;
            if (l != xs.begin() &amp;&amp; *(--l) == x - k - 1) return i + 1;
        }
        
        return -1;
    }
};</pre><p></div></div></p>
<h1><strong>Solution 3: Buckets</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 196 ms (better than 94%)
class Solution {
public:
    int kEmptySlots(vector&lt;int&gt;&amp; flowers, int k) {
        int n = flowers.size();
        if (n == 0 || k &gt;= n) return -1;
        ++k;
        int bs = (n + k - 1) / k;
        vector&lt;int&gt; lows(bs, INT_MAX);
        vector&lt;int&gt; highs(bs, INT_MIN);
        for (int i = 0; i &lt; n; ++i) {
            int x = flowers[i];
            int p = x / k;
            if (x &lt; lows[p]) {
                lows[p] = x;
                if (p &gt; 0 &amp;&amp; highs[p - 1] == x - k) return i + 1;
            } 
            if (x &gt; highs[p]) {
                highs[p] = x;
                if (p &lt; bs - 1 &amp;&amp; lows[p + 1] == x + k) return i + 1;
            }            
        }
        
        return -1;
    }
};</pre><p></div></div></p>
<h1>Solution 1: TLE with latest test cases</h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 192 ms (better than 97.85%)
class Solution {
public:
    int kEmptySlots(vector&lt;int&gt;&amp; flowers, int k) {
        int n = flowers.size();
        if (n == 0 || k &gt;= n) return -1;
        std::unique_ptr&lt;char[]&gt; f(new char[n+1]);
        memset(f.get(), 0, (n + 1)*sizeof(char));
        for (int i = 0; i &lt; n; ++i)
            if (IsValid(flowers[i], k, n, f.get()))
                return i + 1;
        return -1;
    }
private:
    bool IsValid(int x, int k, int n, char* f) {
        f[x] = 1;
        if (x + k + 1 &lt;= n &amp;&amp; f[x + k + 1]) {
            bool valid = true; 
            for (int i = 1; i &lt;= k; ++i)
                if (f[x + i]) {
                    valid = false;
                    break;
                }
            if (valid) return true;
        }
        if (x - k - 1 &gt; 0 &amp;&amp; f[x - k - 1]) {            
            for (int i = 1; i &lt;= k; ++i)
                if (f[x - i]) return false;
            return true;
        }
        return false;
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 17 ms
class Solution {
    public int kEmptySlots(int[] flowers, int k) {
        int n = flowers.length;
        if (n == 0 || k &gt;= n) return -1;
        int[] f = new int[n + 1];
        
        for (int i = 0; i &lt; n; ++i)
            if (IsValid(flowers[i], k, n, f))
                return i + 1;
        
        return -1;
    }
    
    private boolean IsValid(int x, int k, int n, int[] f) {
        f[x] = 1;
        if (x + k + 1 &lt;= n &amp;&amp; f[x + k + 1] == 1) {
            boolean valid = true; 
            for (int i = 1; i &lt;= k; ++i)
                if (f[x + i] == 1) {
                    valid = false;
                    break;
                }
            if (valid) return true;
        }
        if (x - k - 1 &gt; 0 &amp;&amp; f[x - k - 1] == 1) {
            for (int i = 1; i &lt;= k; ++i)
                if (f[x - i] == 1) return false;
            return true;
        }
        return false;
    }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Runtime: 398 ms
"""
class Solution:
    def kEmptySlots(self, flowers, k):
        n = len(flowers)
        f = [0] * (n + 1)
        i = 0
        
        def isValid(x, k, n, f):
            f[x] = 1
            if x + k + 1 &lt;= n and f[x + k + 1] == 1:
                valid = True
                for i in range(k):
                    if f[x + i + 1] == 1: 
                        valid = False
                        break
                if valid: return True
            if x - k - 1 &gt; 0 and f[x - k - 1] == 1:
                for i in range(k):
                    if f[x - i - 1] == 1:
                        return False
                return True
            return False
        
        for x in flowers:
            i += 1
            if isValid(x, k, n, f): return i
        
        return -1</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-683-k-empty-slots/">花花酱 LeetCode 683. K Empty Slots</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/simulation/leetcode-683-k-empty-slots/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
