Skip to content

Commit 3090567

Browse files
leetcode 605 solution
1 parent cbaf2d8 commit 3090567

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

OOPS/leetcode_605.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def canPlaceFlowers(self, nums: List[int], n: int) -> bool:
3+
# flower -> counts how many flowers we can place
4+
flower = 0
5+
6+
# count -> counts continuous empty plots (0s)
7+
# initialized as 1 to handle the left boundary case
8+
count = 1
9+
10+
# traverse through the flowerbed
11+
for r in range(len(nums)):
12+
13+
# if the current plot is empty
14+
if nums[r] == 0:
15+
# increase the count of consecutive empty plots
16+
count += 1
17+
18+
else:
19+
# if we encounter a flower (1),
20+
# calculate how many flowers can be placed in the
21+
# previous sequence of empty plots
22+
flower += (count - 1) // 2
23+
24+
# reset count because the sequence of zeros breaks
25+
count = 0
26+
27+
# handle the right boundary (end of array)
28+
count += 1
29+
30+
# calculate flowers for the last sequence of empty plots
31+
flower += (count - 1) // 2
32+
33+
# return True if we can place at least n flowers
34+
return flower >= n

0 commit comments

Comments
 (0)