forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
35 lines (27 loc) · 763 Bytes
/
main.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
35
/// Source : https://leetcode.com/problems/can-place-flowers/
/// Author : liuyubobobo
/// Time : 2020-12-05
#include <iostream>
#include <vector>
using namespace std;
/// Simulation
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
for(int i = 0; i < flowerbed.size() && n; i ++)
if(flowerbed[i] == 0){
flowerbed[i] = 1;
if(i - 1 >= 0 && flowerbed[i - 1] == 1)
flowerbed[i] = 0;
else if(i + 1 < flowerbed.size() && flowerbed[i + 1] == 1)
flowerbed[i] = 0;
else n --;
}
return n == 0;
}
};
int main() {
return 0;
}