File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments