# [LeetCode 75] DAY 4 - Can Place Flowers
## 題目:
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.
## 解題思考
- 宣告一個計數器 `count`
- 遍歷陣列,檢查當前 `flowerbed[i] === 0`(=== 1跳過)
- 檢查當前 `flowerbed[i]` 的前後是否都等於 0
- `flowerbed[i - 1] === 0 && flowerbed[i + 1] === 0`
- 同時要檢查是否為陣列兩端
- `(i === 0 || flowerbed[i - 1] === 0) && (i === flowerbed.length - 1 || flowerbed[i + 1] === 0)`
- 如果以上都通過,`flowerbed[i] = 1`,`count++`
- 最後比較 `count >= n`
## 程式碼
```javascript
function canPlaceFlowers(flowerbed, n) {
let count = 0;
for (let i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] === 0) {
if ((i === 0 || flowerbed[i - 1] === 0) && (i === flowerbed.length - 1 || flowerbed[i + 1] === 0)) {
flowerbed[i] = 1;
count++;
}
}
}
return count >= n;
}
```
## 延伸思考
- Greedy Algorithm
## 相關資源
- [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/description/)