We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c55a3b9 commit eb2c7c1Copy full SHA for eb2c7c1
cpp/605-Can-Place-Flowers.cpp
@@ -0,0 +1,32 @@
1
+class Solution {
2
+public:
3
+ bool canPlaceFlowers(vector<int>& flowerbed, int n) {
4
+ // pre-check, if n is 0 ... return true
5
+ if(n == 0){
6
+ return true;
7
+ }
8
+
9
+ // add a zero to the front and end of FB
10
+ flowerbed.insert (flowerbed.begin(),0);
11
+ flowerbed.push_back(0);
12
13
+ // iterate through vector (1, vector -1) (for)
14
+ // if prev, curr, and next are 0
15
+ // plant flower (1)
16
+ // decrement n
17
+ for(int i = 1; i < flowerbed.size() - 1; i++){
18
+ if (flowerbed[i - 1] == 0 && flowerbed[i] == 0 && flowerbed[i+1] == 0){
19
+ flowerbed[i] = 1;
20
+ n--;
21
22
+ if (n == 0){
23
24
25
26
27
28
+ // return false as else
29
+ return false;
30
31
32
+};
0 commit comments