-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path605_can_place_flowers.cpp
More file actions
43 lines (40 loc) · 949 Bytes
/
605_can_place_flowers.cpp
File metadata and controls
43 lines (40 loc) · 949 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
33
34
35
36
37
38
39
40
41
42
43
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
class Solution
{
public:
bool canPlaceFlowers(std::vector<int> &flowerbed, int n)
{
if (n == 0)
{
return true;
}
int plantedFlowers = 0;
flowerbed.insert(flowerbed.begin(), {0});
flowerbed.push_back({0});
for (auto it = flowerbed.begin() + 1; it < flowerbed.end() - 1; it++)
{
if ((*(it - 1) == 0) & (*it == 0) & (*(it + 1) == 0))
{
*it = 1;
plantedFlowers++;
if (plantedFlowers >= n)
{
return true;
}
};
}
return false;
}
};
int main()
{
Solution s = Solution();
std::vector<int> flowerbed = {0, 0, 0, 0, 0};
int n = 3;
bool res = s.canPlaceFlowers(flowerbed, n);
std::cout << res << '\n';
return 0;
}