Skip to content

Commit e60ce4e

Browse files
authored
Merge pull request doocs#98 from ashwek/master
0605 Can Place Flowers - Python
2 parents d36f5b5 + 5cb92a1 commit e60ce4e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def canPlaceFlowers(self, flowerBed, n):
3+
"""
4+
type flowerBed : List[int], n : int
5+
rtype : bool
6+
"""
7+
8+
i = 0
9+
while n > 0 and i < len(flowerBed):
10+
if i == 0 and flowerBed[0] == 0: # for 1st Element
11+
if len(flowerBed) == 1 or (len(flowerBed) > 1 and flowerBed[1] == 0):
12+
n -= 1
13+
flowerBed[0] = 1
14+
elif i == len(flowerBed)-1 and flowerBed[i] == 0 and flowerBed[i-1] == 0: # for last element
15+
n -= 1
16+
flowerBed[i] = 1
17+
elif flowerBed[i] == 0 and flowerBed[i-1] == 0 and flowerBed[i+1] == 0:
18+
n -= 1
19+
flowerBed[i] = 1
20+
i += 1
21+
22+
return n==0

0 commit comments

Comments
 (0)