Skip to content

Commit 198dc17

Browse files
committed
Create 0605-can-place-flowers.kt
1 parent 23ed302 commit 198dc17

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Diff for: kotlin/0605-can-place-flowers.kt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
fun canPlaceFlowers(flowerbed: IntArray, n: Int): Boolean {
3+
var planted = 0
4+
for(i in 0 until flowerbed.size) {
5+
if(flowerbed[i] == 0){
6+
val prev = if(i == 0) 0 else flowerbed[i-1]
7+
val next = if(i == flowerbed.size-1) 0 else flowerbed[i+1]
8+
if(prev == 0 && next == 0){
9+
planted++
10+
flowerbed[i] = 1
11+
}
12+
if(planted == n) return true
13+
}
14+
}
15+
return planted >= n
16+
}
17+
}

0 commit comments

Comments
 (0)