Skip to content

Commit fb8d28c

Browse files
committedJan 4, 2025
Add solution #605
1 parent 3208197 commit fb8d28c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
566|[Reshape the Matrix](./0566-reshape-the-matrix.js)|Easy|
206206
567|[Permutation in String](./0567-permutation-in-string.js)|Medium|
207207
599|[Minimum Index Sum of Two Lists](./0599-minimum-index-sum-of-two-lists.js)|Easy|
208+
605|[Can Place Flowers](./0605-can-place-flowers.js)|Easy|
208209
606|[Construct String from Binary Tree](./0606-construct-string-from-binary-tree.js)|Easy|
209210
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
210211
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|

‎solutions/0605-can-place-flowers.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 605. Can Place Flowers
3+
* https://leetcode.com/problems/can-place-flowers/
4+
* Difficulty: Easy
5+
*
6+
* You have a long flowerbed in which some of the plots are planted, and some are not.
7+
* However, flowers cannot be planted in adjacent plots.
8+
*
9+
* Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1
10+
* means not empty, and an integer n, return true if n new flowers can be planted in
11+
* the flowerbed without violating the no-adjacent-flowers rule and false otherwise.
12+
*/
13+
14+
/**
15+
* @param {number[]} flowerbed
16+
* @param {number} n
17+
* @return {boolean}
18+
*/
19+
var canPlaceFlowers = function(flowerbed, n) {
20+
let count = 0;
21+
for (let i = 0; i < flowerbed.length; i++) {
22+
if (flowerbed[i] === 0 && flowerbed[i - 1] !== 1 && flowerbed[i + 1] !== 1) {
23+
count++;
24+
flowerbed[i] = 1;
25+
}
26+
}
27+
return count >= n;
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.