-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCan Place Flowers.py
34 lines (26 loc) · 1.23 KB
/
Can Place Flowers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# https://leetcode.com/problems/can-place-flowers/?envType=study-plan-v2&envId=leetcode-75
"""
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.
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.
"""
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
count = 0
for i in range(len(flowerbed)):
if flowerbed[i] == 0:
empty_left_plot = (i == 0) or (flowerbed[i-1] == 0)
empty_right_plot = (i == len(flowerbed) - 1) or (flowerbed[i+1] == 0)
if empty_left_plot and empty_right_plot:
flowerbed[i] = 1
count += 1
if count >= n:
return True
return count >= n
sol = Solution()
print(sol.canPlaceFlowers([1, 0, 0, 0, 1], 1))
print(sol.canPlaceFlowers([1, 0, 0, 0, 1], 2))