forked from atilsamancioglu/DA2-InterviewChallengeSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanplaceflower.py
More file actions
16 lines (12 loc) · 724 Bytes
/
Copy pathcanplaceflower.py
File metadata and controls
16 lines (12 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'''
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 if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
'''
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
newList = [0] + flowerbed + [0]
for i in range(1,len(newList) - 1): #skip the 0s we added
if newList[i - 1] == 0 and newList[i] == 0 and newList[i+1] == 0:
newList[i] = 1
n -= 1
return n <= 0