💬 Info
- 난이도 : Easy
- 문제 링크 : https://leetcode.com/classic/problems/can-place-flowers/description/
- 풀이 링크 : LeetCode/Easy/Can Place Flowers.java
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array flowerbed
containing 0
's and 1
's, where 0
means empty and 1
means not empty, and an integer n
, return true
if n
new flowers can be planted in the flowerbed
without violating the no-adjacent-flowers rule and false
otherwise.
flowerbed[i]
is 0
or 1
flowerbed
.풀이 시간 : 15분
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
for (int i = 0; i < flowerbed.length && n > 0; i++) {
if (flowerbed[i] == 0
&& (i == 0 || flowerbed[i - 1] == 0)
&& (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
flowerbed[i] = 1;
n--;
}
}
return n == 0;
}
}