-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0683-KEmptySlots.cs
42 lines (36 loc) · 1.08 KB
/
0683-KEmptySlots.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
39
40
41
42
//-----------------------------------------------------------------------------
// Runtime: 260ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0683_KEmptySlots
{
public int KEmptySlots(int[] bulbs, int K)
{
var days = new int[bulbs.Length];
for (int j = 0; j < bulbs.Length; j++)
days[bulbs[j] - 1] = j + 1;
var left = 0;
var right = K + 1;
var result = int.MaxValue;
var i = 1;
while (right < bulbs.Length)
{
if (days[left] < days[i] && days[right] < days[i])
{
i++;
continue;
}
if (i == right)
result = Math.Min(result, Math.Max(days[left], days[right]));
left = i;
right = i + K + 1;
i++;
}
return result < int.MaxValue ? result : -1;
}
}
}