> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# K Empty Slots Python Solution with Tests

> Tested Python solution for LeetCode 683 with 41 pytest cases. Generate a practice environment with lcpy.

LeetCode 683, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Indexed Tree](/catalog/topics/binary-indexed-tree), [Segment Tree](/catalog/topics/segment-tree), [Queue](/catalog/topics/queue), [Ordered Set](/catalog/topics/ordered-set), [Sliding Window](/catalog/topics/sliding-window), Monotonic Queue, [Heap (Priority Queue)](/catalog/topics/heap-priority-queue). [View on LeetCode](https://leetcode.com/problems/k-empty-slots/description/).

Generate this problem as a practice environment: tested reference solution, 41 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 683   # by problem number
lcpy gen -s k_empty_slots   # by problem name
```

## Problem

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 is no such day, return `-1`.*

### Examples

```
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.

```
Input: bulbs = [1,2,3], k = 1
Output: -1
```

### Constraints

* `n == bulbs.length`
* `1 <= n <= 2 * 10^4`
* `1 <= bulbs[i] <= n`
* `bulbs` is a permutation of numbers from `1` to `n`.
* `0 <= k <= 2 * 10^4`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/k_empty_slots/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/k_empty_slots/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def k_empty_slots(self, bulbs: list[int], k: int) -> int:
        n = len(bulbs)
        days = [0] * n
        for day, pos in enumerate(bulbs, 1):
            days[pos - 1] = day

        ans = n + 1
        left, right = 0, k + 1
        while right < n:
            valid = True
            for i in range(left + 1, right):
                if days[i] < days[left] or days[i] < days[right]:
                    left, right = i, i + k + 1
                    valid = False
                    break
            if valid:
                ans = min(ans, max(days[left], days[right]))
                left, right = right, right + k + 1

        return -1 if ans == n + 1 else ans
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.