{"id":471,"date":"2017-09-29T22:31:31","date_gmt":"2017-09-30T05:31:31","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=471"},"modified":"2018-08-30T13:48:09","modified_gmt":"2018-08-30T20:48:09","slug":"leetcode-683-k-empty-slots","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-683-k-empty-slots\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 683. K Empty Slots"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 683. K Empty Slots - \u5237\u9898\u627e\u5de5\u4f5c EP76\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/K8Nk0AiIX4s?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><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u6709n\u4e2a\u82b1\u76c6\uff0c\u7b2ci\u5929\uff0c\u7b2cflowers[i]\u4e2a\u82b1\u76c6\u7684\u82b1\u4f1a\u5f00\u3002\u95ee\u662f\u5426\u5b58\u5728\u4e00\u5929\uff0c\u4e24\u6735\u82b1\u4e4b\u95f4\u6709k\u4e2a\u7a7a\u82b1\u76c6\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>There is a garden with\u00a0<code>N<\/code>\u00a0slots. In each slot, there is a flower. The\u00a0<code>N<\/code>\u00a0flowers will bloom one by one in\u00a0<code>N<\/code>\u00a0days. In each day, there will be\u00a0<code>exactly<\/code>\u00a0one flower blooming and it will be in the status of blooming since then.<\/p>\n<p>Given an array\u00a0<code>flowers<\/code>\u00a0consists of number from\u00a0<code>1<\/code>\u00a0to\u00a0<code>N<\/code>. Each number in the array represents the place where the flower will open in that day.<\/p>\n<p>For example,\u00a0<code>flowers[i] = x<\/code>\u00a0means that the unique flower that blooms at day\u00a0<code>i<\/code>\u00a0will be at position\u00a0<code>x<\/code>, where\u00a0<code>i<\/code>\u00a0and\u00a0<code>x<\/code>\u00a0will be in the range from\u00a0<code>1<\/code>\u00a0to\u00a0<code>N<\/code>.<\/p>\n<p>Also given an integer\u00a0<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\u00a0<code>k<\/code>\u00a0and these flowers are not blooming.<\/p>\n<p>If there isn&#8217;t such day, output -1.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\">Input: \r\nflowers: [1,3,2]\r\nk: 1\r\nOutput: 2\r\nExplanation: In the second day, the first and the third flower have become blooming.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\">Input: \r\nflowers: [1,2,3]\r\nk: 1\r\nOutput: -1\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The given array will be in the range [1, 20000].<\/li>\n<\/ol>\n<p><strong>Idea:<\/strong><\/p>\n<p>BST\/Buckets<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<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 \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/683-ep76.png\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/683-ep76-2.png\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/683-ep76-3.png\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<h1><strong>Solution 2: BST<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 228 ms\r\nclass Solution {\r\npublic:\r\n    int kEmptySlots(vector&lt;int&gt;&amp; flowers, int k) {\r\n        int n = flowers.size();\r\n        if (n == 0 || k &gt;= n) return -1;        \r\n        set&lt;int&gt; xs;        \r\n        for (int i = 0; i &lt; n; ++i) {\r\n            int x = flowers[i];\r\n            auto r = xs.insert(x).first;\r\n            auto l = r;\r\n            if (++r != xs.end() &amp;&amp; *r == x + k + 1) return i + 1;\r\n            if (l != xs.begin() &amp;&amp; *(--l) == x - k - 1) return i + 1;\r\n        }\r\n        \r\n        return -1;\r\n    }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 3: Buckets<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 196 ms (better than 94%)\r\nclass Solution {\r\npublic:\r\n    int kEmptySlots(vector&lt;int&gt;&amp; flowers, int k) {\r\n        int n = flowers.size();\r\n        if (n == 0 || k &gt;= n) return -1;\r\n        ++k;\r\n        int bs = (n + k - 1) \/ k;\r\n        vector&lt;int&gt; lows(bs, INT_MAX);\r\n        vector&lt;int&gt; highs(bs, INT_MIN);\r\n        for (int i = 0; i &lt; n; ++i) {\r\n            int x = flowers[i];\r\n            int p = x \/ k;\r\n            if (x &lt; lows[p]) {\r\n                lows[p] = x;\r\n                if (p &gt; 0 &amp;&amp; highs[p - 1] == x - k) return i + 1;\r\n            } \r\n            if (x &gt; highs[p]) {\r\n                highs[p] = x;\r\n                if (p &lt; bs - 1 &amp;&amp; lows[p + 1] == x + k) return i + 1;\r\n            }            \r\n        }\r\n        \r\n        return -1;\r\n    }\r\n};<\/pre>\n<\/div><\/div>\n<h1>Solution 1: TLE with latest test cases<\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 192 ms (better than 97.85%)\r\nclass Solution {\r\npublic:\r\n    int kEmptySlots(vector&lt;int&gt;&amp; flowers, int k) {\r\n        int n = flowers.size();\r\n        if (n == 0 || k &gt;= n) return -1;\r\n        std::unique_ptr&lt;char[]&gt; f(new char[n+1]);\r\n        memset(f.get(), 0, (n + 1)*sizeof(char));\r\n        for (int i = 0; i &lt; n; ++i)\r\n            if (IsValid(flowers[i], k, n, f.get()))\r\n                return i + 1;\r\n        return -1;\r\n    }\r\nprivate:\r\n    bool IsValid(int x, int k, int n, char* f) {\r\n        f[x] = 1;\r\n        if (x + k + 1 &lt;= n &amp;&amp; f[x + k + 1]) {\r\n            bool valid = true; \r\n            for (int i = 1; i &lt;= k; ++i)\r\n                if (f[x + i]) {\r\n                    valid = false;\r\n                    break;\r\n                }\r\n            if (valid) return true;\r\n        }\r\n        if (x - k - 1 &gt; 0 &amp;&amp; f[x - k - 1]) {            \r\n            for (int i = 1; i &lt;= k; ++i)\r\n                if (f[x - i]) return false;\r\n            return true;\r\n        }\r\n        return false;\r\n    }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 17 ms\r\nclass Solution {\r\n    public int kEmptySlots(int[] flowers, int k) {\r\n        int n = flowers.length;\r\n        if (n == 0 || k &gt;= n) return -1;\r\n        int[] f = new int[n + 1];\r\n        \r\n        for (int i = 0; i &lt; n; ++i)\r\n            if (IsValid(flowers[i], k, n, f))\r\n                return i + 1;\r\n        \r\n        return -1;\r\n    }\r\n    \r\n    private boolean IsValid(int x, int k, int n, int[] f) {\r\n        f[x] = 1;\r\n        if (x + k + 1 &lt;= n &amp;&amp; f[x + k + 1] == 1) {\r\n            boolean valid = true; \r\n            for (int i = 1; i &lt;= k; ++i)\r\n                if (f[x + i] == 1) {\r\n                    valid = false;\r\n                    break;\r\n                }\r\n            if (valid) return true;\r\n        }\r\n        if (x - k - 1 &gt; 0 &amp;&amp; f[x - k - 1] == 1) {\r\n            for (int i = 1; i &lt;= k; ++i)\r\n                if (f[x - i] == 1) return false;\r\n            return true;\r\n        }\r\n        return false;\r\n    }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 398 ms\r\n\"\"\"\r\nclass Solution:\r\n    def kEmptySlots(self, flowers, k):\r\n        n = len(flowers)\r\n        f = [0] * (n + 1)\r\n        i = 0\r\n        \r\n        def isValid(x, k, n, f):\r\n            f[x] = 1\r\n            if x + k + 1 &lt;= n and f[x + k + 1] == 1:\r\n                valid = True\r\n                for i in range(k):\r\n                    if f[x + i + 1] == 1: \r\n                        valid = False\r\n                        break\r\n                if valid: return True\r\n            if x - k - 1 &gt; 0 and f[x - k - 1] == 1:\r\n                for i in range(k):\r\n                    if f[x - i - 1] == 1:\r\n                        return False\r\n                return True\r\n            return False\r\n        \r\n        for x in flowers:\r\n            i += 1\r\n            if isValid(x, k, n, f): return i\r\n        \r\n        return -1\r\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6709n\u4e2a\u82b1\u76c6\uff0c\u7b2ci\u5929\uff0c\u7b2cflowers[i]\u4e2a\u82b1\u76c6\u7684\u82b1\u4f1a\u5f00\u3002\u95ee\u662f\u5426\u5b58\u5728\u4e00\u5929\uff0c\u4e24\u6735\u82b1\u4e4b\u95f4\u6709k\u4e2a\u7a7a\u82b1\u76c6\u3002 Problem: There is a garden with\u00a0N\u00a0slots. In each slot, there is a flower. The\u00a0N\u00a0flowers will bloom one by one in\u00a0N\u00a0days. In each day, there&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,74,115],"class_list":["post-471","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-bst","tag-bucket","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/471","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=471"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":3775,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/471\/revisions\/3775"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}