2022-01-18 13:38:21 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
class Solution {
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
bool canPlaceFlowers(vector<int> &flowerbed, int n) {
|
2022-01-18 13:38:21 +01:00
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
int left = 0, right;
|
|
|
|
for (vector<int>::size_type i = 0; i < flowerbed.size(); i++) {
|
|
|
|
right = (i + 1 < flowerbed.size()) ? flowerbed[i + 1] : 0;
|
|
|
|
|
|
|
|
if (left == 0 && flowerbed[i] == 0 && right == 0) {
|
|
|
|
count++;
|
|
|
|
left = 1;
|
|
|
|
} else {
|
|
|
|
left = flowerbed[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count >= n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
int main() {
|
2022-01-18 13:38:21 +01:00
|
|
|
Solution s;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
std::vector flowers{1, 0, 0, 0, 1};
|
2022-01-18 13:38:21 +01:00
|
|
|
assert(s.canPlaceFlowers(flowers, 1));
|
|
|
|
assert(!s.canPlaceFlowers(flowers, 2));
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
flowers = {1, 0, 0, 0, 0, 1};
|
2022-01-18 13:38:21 +01:00
|
|
|
assert(!s.canPlaceFlowers(flowers, 2));
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
flowers = {1, 0, 0, 0, 1, 0, 0};
|
2022-01-18 13:38:21 +01:00
|
|
|
assert(s.canPlaceFlowers(flowers, 2));
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
flowers = {0, 0, 1, 0, 0};
|
2022-01-18 13:38:21 +01:00
|
|
|
assert(s.canPlaceFlowers(flowers, 1));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|