forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0605-can-place-flowers.cpp
More file actions
32 lines (27 loc) · 835 Bytes
/
0605-can-place-flowers.cpp
File metadata and controls
32 lines (27 loc) · 835 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
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
// pre-check, if n is 0 ... return true
if(n == 0){
return true;
}
// add a zero to the front and end of FB
flowerbed.insert (flowerbed.begin(),0);
flowerbed.push_back(0);
// iterate through vector (1, vector -1) (for)
// if prev, curr, and next are 0
// plant flower (1)
// decrement n
for(int i = 1; i < flowerbed.size() - 1; i++){
if (flowerbed[i - 1] == 0 && flowerbed[i] == 0 && flowerbed[i+1] == 0){
flowerbed[i] = 1;
n--;
}
if (n == 0){
return true;
}
}
// return false as else
return false;
}
};