Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2d1410c

Browse files
committed
Add can place flowers
1 parent ee61486 commit 2d1410c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

‎array_string/can_place_flowers/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
6+
counter = 0
7+
8+
for index in range(len(flowerbed)):
9+
if flowerbed[index] == 0:
10+
left_is_empty = (index == 0) or (flowerbed[index - 1] == 0)
11+
right_is_empty = (index == len(flowerbed) - 1) or (flowerbed[index + 1] == 0)
12+
if left_is_empty and right_is_empty:
13+
flowerbed[index] = 1
14+
counter += 1
15+
if counter >= n:
16+
return True
17+
18+
return counter >= n

‎tests/test_can_place_flowers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
from array_string.can_place_flowers.solution import Solution
3+
4+
@pytest.mark.parametrize("flowerbed, n, expected", [
5+
([1, 0, 0, 0, 1], 1, True),
6+
([1, 0, 0, 0, 1], 2, False),
7+
([1,0,1,0,1,0,1], 1, False),
8+
([1, 0, 0, 0, 1, 0, 0], 2, True),
9+
([0], 1, True),
10+
([1,0,1,0,1,0,1], 0, True)
11+
])
12+
def test_merge_alternately(flowerbed, n, expected):
13+
solution = Solution()
14+
assert solution.canPlaceFlowers(flowerbed, n) == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /