# 605.can-place-flowers

## Statement

• Difficulty: Easy
• Tag: `贪心` `数组`

``````输入：flowerbed = [1,0,0,0,1], n = 1

``````

``````输入：flowerbed = [1,0,0,0,1], n = 2

``````

• `1 <= flowerbed.length <= 2 * 104`
• `flowerbed[i]``0``1`
• `flowerbed` 中不存在相邻的两朵花
• `0 <= n <= flowerbed.length`

• Difficulty: Easy
• Tag: `Greedy` `Array`

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 if `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule.

Example 1:

``````Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
``````

Example 2:

``````Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
``````

Constraints:

• `1 <= flowerbed.length <= 2 * 104`
• `flowerbed[i]` is `0` or `1`.
• There are no two adjacent flowers in `flowerbed`.
• `0 <= n <= flowerbed.length`

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class Solution {
public:
bool canPlaceFlowers(vector<int> &f, int n) {
int M = 0;
int m = f.size();
for (int i = 0; i < m; i++) {
if (f[i] == 1) {
continue;
}

if ((i == 0 || f[i - 1] == 0) && (i == m - 1 || f[i + 1] == 0)) {
f[i] = 1;
++M;
}
}

return M >= n;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````