Skip to content

Commit 762c218

Browse files
solves can place flowers
1 parent e0e9d72 commit 762c218

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii) | [![Java](assets/java.png)](src/RangeAdditionII.java) [![Python](assets/python.png)](python/range_addition_ii.py) |
160160
| 599 | [Minimum Index Sum of 2 Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists) | [![Java](assets/java.png)](src/MinimumIndexSumOfTwoLists.java) [![Python](assets/python.png)](python/minimum_index_sum_of_two_lists.py) |
161161
| 604 | 🔒 [Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator) | |
162-
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers) | |
162+
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers) | [![Java](assets/java.png)](src/CanPlaceFlowers.java) [![Python](assets/python.png)](python/can_place_flowers.py) |
163163
| 606 | [Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree) | |
164164
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees) | |
165165
| 624 | [Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays) | |

Diff for: python/can_place_flowers.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def flowerBedCapacity(self, flowerbed: List[int]) -> int:
6+
capacity = 0
7+
for index, value in enumerate(flowerbed):
8+
if value != 1 and (index == 0 or flowerbed[index - 1] == 0) and (index == len(flowerbed) - 1 or flowerbed[index + 1] == 0):
9+
capacity += 1
10+
flowerbed[index] = 1
11+
return capacity
12+
13+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
14+
return self.flowerBedCapacity(flowerbed) >= n

Diff for: src/CanPlaceFlowers.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class CanPlaceFlowers {
2+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
3+
return flowersCapacity(flowerbed) >= n;
4+
}
5+
6+
private int flowersCapacity(int[] flowerbed) {
7+
int capacity = 0;
8+
for (int index = 0 ; index < flowerbed.length ; index++) {
9+
if ((flowerbed[index] != 1) && (index == 0 || flowerbed[index - 1] == 0) && (index == flowerbed.length - 1 || flowerbed[index + 1] == 0)) {
10+
capacity++;
11+
flowerbed[index] = 1;
12+
}
13+
}
14+
return capacity;
15+
}
16+
}

0 commit comments

Comments
 (0)