-
Notifications
You must be signed in to change notification settings - Fork 0
/
Can place flowers(Leetcode).cpp
34 lines (32 loc) · 1.04 KB
/
Can place flowers(Leetcode).cpp
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
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
// we need to find that how many maximum flowers can be planted
int size=flowerbed.size();
// initialize the count of zero
int zeroes=1, ans=0;
if(n==0)return true;
for(int i=0;i<size;i++){
if(flowerbed[i]==0)++zeroes;
else{
// number of zeroes possible
ans+= (zeroes-1)/2;
zeroes=0;
}
}
return ans+ (zeroes)/2 >=n;
}
// class Solution {
// public:
// bool canPlaceFlowers(vector<int>& flowerbed, int n) {
// if (n == 0) return true;
// for (int i = 0; i < flowerbed.size(); i ++) {
// if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.size() - 1 || flowerbed[i + 1] == 0)) { // can place?
// -- n;
// if (n == 0) return true;
// flowerbed[i] = 1; // place!
// }
// }
// return false;
// }
};