Skip to content

Commit 9de4afa

Browse files
committed
605. Can Place Flowers
1 parent f368f77 commit 9de4afa

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 605. Can Place Flowers
2+
# You have a long flowerbed in which some of the plots are planted, and some are not.
3+
# However, flowers cannot be planted in adjacent plots.
4+
5+
# Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty,
6+
# and an integer n, return true if n new flowers can be planted in the flowerbed without violating
7+
# the no-adjacent-flowers rule and false otherwise.
8+
9+
# Example 1:
10+
11+
# Input: flowerbed = [1,0,0,0,1], n = 1
12+
# Output: true
13+
# Example 2:
14+
15+
# Input: flowerbed = [1,0,0,0,1], n = 2
16+
# Output: false
17+
18+
def canPlaceFlowers(flowerbed, n):
19+
if n==0: return True
20+
21+
for i in range(len(flowerbed)):
22+
if flowerbed[i]==0 and (i==0 or flowerbed[i-1]==0) and (i==len(flowerbed)-1 or flowerbed[i+1]==0):
23+
flowerbed[i]=1
24+
n-=1
25+
if n==0: return True
26+
return False

0 commit comments

Comments
 (0)