Can Place Flowers LeetCode solution in Python

1
class Solution:
2
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
3
f = [0] + flowerbed + [0]
4

5
i, could_plant = 1, 0
6
while could_plant < n and i < len(f) - 1:
7
if f[i + 1]:
8
# 0 0 1 -> skip 3
9
i += 3
10
elif f[i]:
11
# 0 1 0 -> skip 2
12
i += 2
13
elif f[i - 1]:
14
# 1 0 0 -> skip 1
15
i += 1
16
else:
17
# 0 0 0 -> plant, becomes 0 1 0 -> skip 2
18
could_plant += 1
19
i += 2
20

21
return n <= could_plant

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0