There are n bulbs in a row at positions 1 to n, and all of them start off. Exactly one bulb is turned on each day, so after n days every bulb is on.
You are given an array bulbs of length n where bulbs[i] = x means that on day i + 1 the bulb at position x is turned on (i is 0-indexed, x is 1-indexed).
Given an integer k, return the minimum day number such that there exist two turned-on bulbs with exactly k bulbs between them, all of which are turned off. If there is no such day, return -1.
Function Signature
def k_empty_slots(bulbs: list[int], k: int) -> int:
Rules
-
Days are numbered from 1: the bulb at position
bulbs[0]
is turned on on day 1.
-
The two bulbs must be at positions
x
and
x + k + 1
, both on, with every bulb at positions
x + 1
to
x + k
still off on that day.
-
With
k = 0
, the condition is two adjacent bulbs that are both on.
Constraints
-
n == len(bulbs)
-
1 <= n <= 2 * 10^4
-
1 <= bulbs[i] <= n
-
bulbs
is a permutation of the integers
1
to
n
.
-
0 <= k <= 2 * 10^4
Examples
Input: bulbs = [1,3,2], k = 1
Output: 2
On day 1 only bulb 1 is on. On day 2 bulb 3 turns on, and bulbs 1 and 3 have exactly one bulb (position 2) between them, which is still off.
Input: bulbs = [1,2,3], k = 1
Output: -1
The bulbs turn on from left to right, so each new bulb sits next to the previous one. By the time bulbs 1 and 3 are both on (day 3), bulb 2 is already on, so no day qualifies.
Input: bulbs = [2,4,1,3], k = 0
Output: 3
After day 2 the bulbs at positions 2 and 4 are on, but they are not adjacent. On day 3 bulb 1 turns on next to bulb 2, which gives the first pair of adjacent lit bulbs.