KatInterview

683. K Empty Slots

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the earliest day on which there are two turned-on bulbs separated by exactly k turned-off bulbs. The input bulbs tells us on which day each bulb is turned on. bulbs[i] = x means on day i+1, the bulb at position x is turned on.

Solution

K Empty Slots in Python

class Solution:
    def kEmptySlots(self, bulbs: List[int], k: int) -> int:
        n = len(bulbs)
        if k >= n:
            return -1

        # days[i] is the day bulb at position i+1 is turned on.
        days = [0] * n
        for i, bulb_pos in enumerate(bulbs):
            # bulbs are 1-indexed, days array is 0-indexed.
            days[bulb_pos - 1] = i + 1

        ans = float('inf')
        left, right = 0, k + 1

        while right < n:
            # The day when both bulbs at `left` and `right` are on.
            turn_on_day = max(days[left], days[right])
            is_valid = True
            
            # Check all bulbs between left and right.
            for i in range(left + 1, right):
                if days[i] <= turn_on_day:
                    # This bulb `i` is a blocker. It's turned on no later than
                    # the bulbs at `left` and `right`. So, the window `[left, right]`
                    # is invalid. The next potential window must start after this blocker.
                    left = i
                    right = i + k + 1
                    is_valid = False
                    break
            
            if is_valid:
                # This is a valid window.
                ans = min(ans, turn_on_day)
                
                # Any window starting between `left` and `right` will be invalid
                # because `right` will be an intermediate bulb turned on too early.
                # So, we can jump the window start to `right`.
                left = right
                right = left + k + 1

        return ans if ans != float('inf') else -1

K Empty Slots in Java

class Solution {
    public int kEmptySlots(int[] bulbs, int k) {
        int n = bulbs.length;
        if (k >= n) {
            return -1;
        }

        // days[i] is the day bulb at position i+1 is turned on.
        int[] days = new int[n];
        for (int i = 0; i < n; i++) {
            // bulbs[i] is 1-indexed, our days array is 0-indexed.
            days[bulbs[i] - 1] = i + 1;
        }

        int ans = Integer.MAX_VALUE;
        int left = 0, right = k + 1;

        while (right < n) {
            // The day when both bulbs at `left` and `right` are on.
            int turnOnDay = Math.max(days[left], days[right]);
            boolean isValid = true;

            // Check all bulbs between left and right.
            for (int i = left + 1; i < right; i++) {
                if (days[i] <= turnOnDay) {
                    // This bulb `i` is a blocker. It's turned on no later than
                    // the bulbs at `left` and `right`. So, the window `[left, right]`
                    // is invalid. The next potential window must start after this blocker.
                    left = i;
                    right = i + k + 1;
                    isValid = false;
                    break;
                }
            }

            if (isValid) {
                // This is a valid window.
                ans = Math.min(ans, turnOnDay);

                // Any window starting between `left` and `right` will be invalid
                // because `right` will be an intermediate bulb turned on too early.
                // So, we can jump the window start to `right`.
                left = right;
                right = left + k + 1;
            }
        }

        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
}

K Empty Slots in C++

class Solution {
public:
    int kEmptySlots(vector<int>& bulbs, int k) {
        int n = bulbs.size();
        if (k >= n) {
            return -1;
        }

        // days[i] is the day bulb at position i+1 is turned on.
        vector<int> days(n);
        for (int i = 0; i < n; ++i) {
            // bulbs[i] is 1-indexed, our days array is 0-indexed
            days[bulbs[i] - 1] = i + 1;
        }

        int ans = INT_MAX;
        int left = 0, right = k + 1;

        while (right < n) {
            // The day when both bulbs at `left` and `right` are on.
            int turnOnDay = max(days[left], days[right]);
            bool isValid = true;

            // Check all bulbs between left and right.
            for (int i = left + 1; i < right; ++i) {
                if (days[i] <= turnOnDay) {
                    // This bulb `i` is a blocker. It's turned on no later than
                    // the bulbs at `left` and `right`. So, the window `[left, right]`
                    // is invalid. The next potential window must start after this blocker.
                    left = i;
                    right = i + k + 1;
                    isValid = false;
                    break;
                }
            }

            if (isValid) {
                // This is a valid window.
                ans = min(ans, turnOnDay);

                // Any window starting between `left` and `right` will be invalid
                // because `right` will be an intermediate bulb turned on too early.
                // So, we can jump the window start to `right`.
                left = right;
                right = left + k + 1;
            }
        }

        return ans == INT_MAX ? -1 : ans;
    }
};

K Empty Slots in TypeScript

function kEmptySlots(bulbs: number[], k: number): number {
    const n = bulbs.length;
    if (k >= n) {
        return -1;
    }

    // days[i] is the day bulb at position i+1 is turned on.
    const days = new Array(n).fill(0);
    for (let i = 0; i < n; i++) {
        // bulbs[i] is 1-indexed, our days array is 0-indexed.
        days[bulbs[i] - 1] = i + 1;
    }

    let ans = Infinity;
    let left = 0;
    let right = k + 1;

    while (right < n) {
        // The day when both bulbs at `left` and `right` are on.
        const turnOnDay = Math.max(days[left], days[right]);
        let isValid = true;

        // Check all bulbs between left and right.
        for (let i = left + 1; i < right; i++) {
            if (days[i] <= turnOnDay) {
                // This bulb `i` is a blocker. It's turned on no later than
                // the bulbs at `left` and `right`. So, the window `[left, right]`
                // is invalid. The next potential window must start after this blocker.
                left = i;
                right = i + k + 1;
                isValid = false;
                break;
            }
        }

        if (isValid) {
            // This is a valid window.
            ans = Math.min(ans, turnOnDay);

            // Any window starting between `left` and `right` will be invalid
            // because `right` will be an intermediate bulb turned on too early.
            // So, we can jump the window start to `right`.
            left = right;
            right = left + k + 1;
        }
    }

    return ans === Infinity ? -1 : ans;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.