Skip to content

Commit c1fd178

Browse files
authored
Create LeetCode 605 - Can Place Flowers.cpp
1 parent 807cba0 commit c1fd178

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//Question link: https://leetcode.com/problems/can-place-flowers/description/
2+
3+
class Solution {
4+
public:
5+
bool checkAdjacentPlots(int prev, int current, int next) {
6+
if ((prev == 0 || prev == -1) && current == 0 && (next == 0 || next == -1))
7+
return true;
8+
return false;
9+
}
10+
11+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
12+
int unplacedFlowers = n;
13+
int size = flowerbed.size();
14+
for (int i = 0; i < size; i++) {
15+
if (flowerbed[i] == 1) continue;
16+
17+
int prev = (i == 0) ? -1 : flowerbed[i - 1];
18+
int next = (i == size - 1) ? -1 : flowerbed[i + 1];
19+
20+
if (checkAdjacentPlots(prev, flowerbed[i], next)) {
21+
unplacedFlowers--;
22+
flowerbed[i] = 1;
23+
}
24+
}
25+
26+
return unplacedFlowers <= 0;
27+
}
28+
};

0 commit comments

Comments
 (0)