Search⌘ K
AI Features

Solution: K Empty Slots

Explore how to solve the K Empty Slots problem by mapping bulb positions to activation days and using a min heap to track days efficiently. This lesson teaches you to find the earliest day two bulbs are on with exactly k bulbs off between them, applying a windowed approach and monotonic queue for optimal time and space complexity.

Statement

You are given nn bulbs arranged in a row, numbered from 11 to nn. Initially, all bulbs are turned off.

Each day, exactly one bulb is switched on. You are given an array, bulbs of length nn where bulbs[i] =x= x means that on day i+1i + 1 (1‑indexed), the bulb at position xx (also 1‑indexed) is turned on.

So, given an integer k, determine the earliest day (the smallest day number) on which there are two bulbs that are on such that exactly k bulbs are off between them.

If no such day exists, return 1 ...