Suppose you have a very long flowerbed, with some plots planted with flowers and some plots not. However, flowers can not be planted on adjacent plots, they will compete for water, and both will die.

Given a flower bed (represented by an array containing 0 and 1, where 0 means no flowers are planted and 1 means flowers are planted), and a number n. Can you plant more than one flower without breaking the planting rules? Can return true, can not return false.

Source: leetcode

Link: https://leetcode-cn.com/problems/can-place-flowers

My thinking:

**In fact, it is to change n zeros in the array into 1, and all 1 cannot be adjacent, so it is necessary to judge the number of continuous zeros.**

**Next, we need to study how many flowers can be planted in the string of 0 with the length of n.**

**Note: special consideration (boundary condition) may be required for the starting and ending continuous zero strings.**

**Note: need to consider the situation of all 0, the first submission was this pit!!!!!!!!**

**My own conclusion is: if the length is n with 0 strings at the beginning or at the end, it can support at most N / 2 flowers; if it is in the middle, it can support at most (n-1) / 2 flowers.**

**I didn’t think of the idea of defense. If I add 0 on both sides directly, I don’t need to consider the boundary. In this way, every three consecutive 0 can grow a flower. Just count the number of 0 elements that are 0 on both sides.**

My answer: bool can place flowers (vector)& flowerbed, int n) {

```
Vector continouszero; // a container that stores all 0-string-length containers
int size=flowerbed.size();
Int count = 0; // the number of zeros
int maxsize=0;
for (int i = 0; i < size; ++i) {
if (flowerbed.at(i)==0) count++;
else
{
continousZero.push_back(count);
count=0;
}
}
continousZero.push_ Back (count); // if you want to add this sentence, otherwise it ends with 0, the length of the last string with 0 will not be stored in the container, and you can distinguish whether it ends with 1 or not.
for (int i = 0; i < continousZero.size(); ++i) {
if (i==0||i==continousZero.size()-1) maxsize+=continousZero.at(i)/2;
else maxsize+=(continousZero.at(i)-1)/2;
}
If (flowerbed = = vector (size, 0)) // judgment of all zeros
{
return (size+1)*0.5>=n;
}
return maxsize>=n;
}
```