-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130CanPlaceFlowers.cpp
More file actions
38 lines (30 loc) · 830 Bytes
/
Copy path130CanPlaceFlowers.cpp
File metadata and controls
38 lines (30 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int count = 0;
for (int i = 0; i < flowerbed.size(); i++) {
if (flowerbed[i] == 0) {
int left = (i == 0) ? 0 : flowerbed[i - 1];
int right = (i == flowerbed.size() - 1) ? 0 : flowerbed[i + 1];
if (left == 0 && right == 0) {
flowerbed[i] = 1;
count++;
}
}
if (count >= n) return true;
}
return count >= n;
}
};
int main() {
Solution obj;
vector<int> flowerbed = {1, 0, 0, 0, 1};
int n = 1;
if (obj.canPlaceFlowers(flowerbed, n))
cout << "true";
else
cout << "false";
return 0;
}