{"id":2953,"date":"2018-06-28T00:10:30","date_gmt":"2018-06-28T07:10:30","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2953"},"modified":"2018-06-28T21:58:39","modified_gmt":"2018-06-29T04:58:39","slug":"leetcode-605-can-place-flowers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-605-can-place-flowers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 605. Can Place Flowers"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots &#8211; they would compete for water and both would die.<\/p>\n<p>Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number\u00a0<b>n<\/b>, return if\u00a0<b>n<\/b>\u00a0new flowers can be planted in it without violating the no-adjacent-flowers rule.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> flowerbed = [1,0,0,0,1], n = 1\r\n<b>Output:<\/b> True\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> flowerbed = [1,0,0,0,1], n = 2\r\n<b>Output:<\/b> False\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The input array won&#8217;t violate no-adjacent-flowers rule.<\/li>\n<li>The input array size is in the range of [1, 20000].<\/li>\n<li><b>n<\/b>\u00a0is a non-negative integer which won&#8217;t exceed the input array size.<\/li>\n<\/ol>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 21 ms\r\nclass Solution {\r\npublic:\r\n  bool canPlaceFlowers(vector&lt;int&gt;&amp; flowerbed, int n) {\r\n    int count = 0;\r\n    for (size_t i = 0; i &lt; flowerbed.size() &amp;&amp; count &lt; n; ++i) {\r\n      if (flowerbed[i]) continue;\r\n      bool left = i == 0 ? true : !flowerbed[i - 1];\r\n      bool right = i == flowerbed.size() - 1 ? true : !flowerbed[i + 1];\r\n      if (left &amp;&amp; right) {\r\n        flowerbed[i++] = 1;\r\n        ++count;\r\n      }\r\n    }\r\n    return count == n;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,51,48],"tags":[],"class_list":["post-2953","post","type-post","status-publish","format-standard","hentry","category-array","category-greedy","category-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2953","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=2953"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2953\/revisions"}],"predecessor-version":[{"id":2956,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2953\/revisions\/2956"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}