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 52dc0cc

Browse files
committed
Solve 605. Can Place Flowers
1 parent 9110c21 commit 52dc0cc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎06/0605-Can_Place_Flowers.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
3+
fun canPlaceFlowers(flowerbed: IntArray, n: Int): Boolean {
4+
if (n == 0) return true
5+
6+
val lastIndex = flowerbed.size - 1
7+
var planted = 0
8+
9+
for (i in 0 until flowerbed.size) {
10+
if (
11+
flowerbed[i] == 0 &&
12+
(i == 0 || flowerbed[i - 1] == 0) &&
13+
(i == lastIndex || flowerbed[i + 1] == 0)
14+
) {
15+
flowerbed[i] = 1
16+
planted++
17+
18+
if (planted == n) {
19+
return true
20+
}
21+
}
22+
}
23+
24+
return false
25+
}
26+
27+
}

0 commit comments

Comments
(0)

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