-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0605-can-place-flowers.py
40 lines (34 loc) · 1.44 KB
/
0605-can-place-flowers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# Solution with O(1) space complexity
empty = 0 if flowerbed[0] else 1
for f in flowerbed:
if f:
n -= int((empty - 1) / 2) # int division, round toward zero
empty = 0
else:
empty += 1
n -= (empty) // 2
return n <= 0
class Solution2:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# Another solution with O(1) space complexity
for i in range(len(flowerbed)):
if n == 0:
return True
if ((i == 0 or flowerbed[i - 1] == 0) # If at the first element or the previous element equals to 0
and (flowerbed[i] == 0) # If current element equals to 0
and (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0)): # If at the last element or the next element equals to 0
# Place flower at the current position
flowerbed[i] = 1
n -= 1
return n == 0
class Solution3:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
# Solution with O(n) space complexity
f = [0] + flowerbed + [0]
for i in range(1, len(f) - 1): # skip first & last
if f[i - 1] == 0 and f[i] == 0 and f[i + 1] == 0:
f[i] = 1
n -= 1
return n <= 0