-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (27 loc) · 758 Bytes
/
Solution.cs
File metadata and controls
29 lines (27 loc) · 758 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
public class Solution
{
public bool CanPlaceFlowers(int[] flowerbed, int n)
{
if (2 * n > flowerbed.Length + 1) return false;
int count = 0;
for (int i = 0; i < flowerbed.Length; i++)
{
if (flowerbed[i] == 0)
{
bool isEmptyLeft = i == 0 || flowerbed[i - 1] == 0;
bool isEmptyRight = i == flowerbed.Length - 1 || flowerbed[i + 1] == 0;
if (isEmptyLeft && isEmptyRight)
{
count++;
flowerbed[i] = 1;
if (count >= n) return true;
}
}
else
{
i++;
}
}
return count >= n;
}
}