Can Place Flowers

Similar Problems:

- CheatSheet: Leetcode For Code Interview
- CheatSheet: Common Code Problems & Follow-ups
- Tag: #manydetails, #game, #padplaceholder

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 – they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.

Example 1: Input: flowerbed = [1,0,0,0,1], n = 1 Output: True

Example 2: Input: flowerbed = [1,0,0,0,1], n = 2 Output: False

Note:

- The input array won’t violate no-adjacent-flowers rule.
- The input array size is in the range of [1, 20000].
- n is a non-negative integer which won’t exceed the input array size.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution greedy

## https://code.dennyzhang.com/can-place-flowers ## Basic Ideas: greedy ## ## Try to put flower for the early 0s. ## This strategy won't make the choices worse ## ## Complexity: Time O(n), Space O(1) class Solution: def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: if n == 0: return True size = len(flowerbed) for i in range(size): if flowerbed[i] == 1: continue # whether we can put a flower if i>0 and flowerbed[i-1] == 1: continue if i+1<size and flowerbed[i+1] == 1: continue # place a flower flowerbed[i] = 1 n -= 1 if n<=0: return True return False

## https://code.dennyzhang.com/can-place-flowers ## Basic Ideas: Find consecutive 0s ## For a range of n 0s ## If the range in the middle, we can place (n-1)/2 flowers ## Add a virtual '0' to the head, add a virtual '0' and '1' to the end ## ## Complexity: Time O(n), Space O(1) class Solution(object): def canPlaceFlowers(self, flowerbed, n): """ :type flowerbed: List[int] :type n: int :rtype: bool """ total_flower, counter = 0, 0 length = len(flowerbed) for i in range(-1, length+2): if i == -1 or i == length: num = 0 elif i == length + 1: num = 1 else: num = flowerbed[i] # caculate how many flowers we can plant if num == 0: counter += 1 else: total_flower, counter = total_flower + (counter-1)/2, 0 return total_flower >= n # s = Solution() # s = s.canPlaceFlowers([0,0,1,0,1], 1) # true