-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem605.cs
38 lines (34 loc) · 1012 Bytes
/
Problem605.cs
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
namespace Problems;
public sealed class Problem605
{
// https://leetcode.com/problems/can-place-flowers/description/
public bool CanPlaceFlowers(int[] flowerbed, int n)
{
if (n == 0) return true;
if (flowerbed.Length == 1)
return flowerbed[0] == 0 && n == 1;
var valids = 0;
for (var i = 0; i < flowerbed.Length; i++)
{
if (flowerbed[i] == 0)
{
if (i == 0 && flowerbed[i + 1] == 0)
{
valids++;
flowerbed[i] = 1;
}
else if (i == flowerbed.Length - 1 && flowerbed[i - 1] == 0)
{
valids++;
flowerbed[i] = 1;
}
else if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0)
{
valids++;
flowerbed[i] = 1;
}
}
}
return n <= valids;
}
}