K Empty Slots Problem
Description
LeetCode Problem 683.
You have n bulbs in a row numbered from 1 to n. Initially, all the bulbs are turned off. We turn on exactly one bulb every day until all bulbs are on after n days.
You are given an array bulbs of length n where bulbs[i] = x means that on the (i+1)^th day, we will turn on the bulb at position x where i is 0-indexed and x is 1-indexed.
Given an integer k, return the minimum day number such that there exists two turned on bulbs that have exactly k bulbs between them that are all turned off. If there isn't such day, return -1.
Example 1:
Input: bulbs = [1,3,2], k = 1
Output: 2
Explanation:
On the first day: bulbs[0] = 1, first bulb is turned on: [1,0,0]
On the second day: bulbs[1] = 3, third bulb is turned on: [1,0,1]
On the third day: bulbs[2] = 2, second bulb is turned on: [1,1,1]
We return 2 because on the second day, there were two on bulbs with one off bulb between them.
Example 2:
Input: bulbs = [1,2,3], k = 1
Output: -1
Constraints:
n == bulbs.length1 <= n <= 2 * 10^41 <= bulbs[i] <= nbulbsis a permutation of numbers from1ton.0 <= k <= 2 * 10^4
Solution
Approach: Binary Indexed Tree
We want the first day on which two bulbs that are on have exactly k bulbs between them, all of them off. Positions x and x + k + 1 must be on, and nothing in between.
A new pair can only appear on the day its second bulb turns on. So when bulb x turns on, we check only two candidates: the bulb k + 1 to its left (x - k - 1) and the one k + 1 to its right (x + k + 1). For each one, the pair works if that bulb is already on and no bulb strictly between the two is on.
To count the bulbs that are on in a range quickly, we keep a Binary Indexed Tree (Fenwick tree) over the positions. Turning a bulb on adds 1 at its position, and query(x) returns how many bulbs are on in 1..x. The bulbs between y and x are all off when query(x - 1) - query(y) == 0.
This check runs when the pair first forms. Once a bulb between them turns on, the pair can never work again, so checking later would not help. Remember to check that the candidate is inside 1..n: k can be larger than n.
Complexity: O(n log n) time and O(n) extra space.
class BinaryIndexedTree:
"""Counts the bulbs that are on; query(x) = bulbs on at positions 1..x."""
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def update(self, x, delta):
while x <= self.n:
self.c[x] += delta
x += x & -x
def query(self, x):
s = 0
while x > 0:
s += self.c[x]
x -= x & -x
return s
class Solution:
def kEmptySlots(self, bulbs: List[int], k: int) -> int:
n = len(bulbs)
tree = BinaryIndexedTree(n)
on = [False] * (n + 2)
for day, x in enumerate(bulbs, 1):
tree.update(x, 1)
on[x] = True
# A new pair can only involve x: look k + 1 to the left and to the right,
# and check that no bulb between the two is on.
y = x - k - 1
if y >= 1 and on[y] and tree.query(x - 1) - tree.query(y) == 0:
return day
y = x + k + 1
if y <= n and on[y] and tree.query(y - 1) - tree.query(x) == 0:
return day
return -1