Leetcode Problem 683. K Empty Slots

683. K Empty Slots

Leetcode Solutions

Sliding Window Approach

  1. Transform the bulbs array into a days array where days[i] is the day the bulb at position i+1 turns on.
  2. Initialize two pointers left and right to represent the window's boundaries, with right = left + k + 1.
  3. Iterate through the days array using a variable i.
  4. If days[i] is greater than both days[left] and days[right], continue to the next iteration.
  5. If i equals right, update the answer with max(days[left], days[right]) as a potential answer.
  6. If days[i] is less than or equal to days[left] or days[right], move left to i and right to left + k + 1.
  7. Repeat steps 3-6 until right is out of bounds.
  8. If an answer was found, return it; otherwise, return -1.
UML Thumbnail

Ordered Set Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...