forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
33 lines (32 loc) · 837 Bytes
/
Solution.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
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int i = 0, j = flowerbed.size()-1 ;
int cnt=0 ;
int num ;
while (i <= j && !flowerbed[i])
++i ;
if (i > j)
return n <= 1 + (j>>1) ;
num = i>>1 ;
while (!flowerbed[j])
--j ;
num += (flowerbed.size()-1-j)>>1 ;
//cout << i << ' ' << j << endl ;
while (i <= j)
{
//cout << "num = " << num << ", cnt = " << cnt << endl ;
if (flowerbed[i])
{
if (cnt > 0)
num += (cnt-1) >> 1 ;
cnt = 0 ;
}
else
cnt++ ;
++i ;
}
//cout << num << endl ;
return num >= n ;
}
};