Skip to content

Commit 912a1db

Browse files
committed
Sync LeetCode submission Runtime - 176 ms (57.84%), Memory - 19.6 MB (70.41%)
1 parent c58fa08 commit 912a1db

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

0683-k-empty-slots/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p>You have <code>n</code> bulbs in a row numbered from <code>1</code> to <code>n</code>. Initially, all the bulbs are turned off. We turn on <strong>exactly one</strong> bulb every day until all bulbs are on after <code>n</code> days.</p>
2+
3+
<p>You are given an array <code>bulbs</code>&nbsp;of length <code>n</code>&nbsp;where <code>bulbs[i] = x</code> means that on the <code>(i+1)<sup>th</sup></code> day, we will turn on the bulb at position <code>x</code>&nbsp;where&nbsp;<code>i</code>&nbsp;is&nbsp;<strong>0-indexed</strong>&nbsp;and&nbsp;<code>x</code>&nbsp;is&nbsp;<strong>1-indexed.</strong></p>
4+
5+
<p>Given an integer <code>k</code>, return&nbsp;<em>the <strong>minimum day number</strong> such that there exists two <strong>turned on</strong> bulbs that have <strong>exactly</strong>&nbsp;<code>k</code> bulbs between them that are <strong>all turned off</strong>. If there isn&#39;t such day, return <code>-1</code>.</em></p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> bulbs = [1,3,2], k = 1
12+
<strong>Output:</strong> 2
13+
<b>Explanation:</b>
14+
On the first day: bulbs[0] = 1, first bulb is turned on: [1,0,0]
15+
On the second day: bulbs[1] = 3, third bulb is turned on: [1,0,1]
16+
On the third day: bulbs[2] = 2, second bulb is turned on: [1,1,1]
17+
We return 2 because on the second day, there were two on bulbs with one off bulb between them.</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> bulbs = [1,2,3], k = 1
23+
<strong>Output:</strong> -1
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>n == bulbs.length</code></li>
31+
<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
32+
<li><code>1 &lt;= bulbs[i] &lt;= n</code></li>
33+
<li><code>bulbs</code>&nbsp;is a permutation of numbers from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>.</li>
34+
<li><code>0 &lt;= k &lt;= 2 * 10<sup>4</sup></code></li>
35+
</ul>

0683-k-empty-slots/solution.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Approach 1: Insert Into Sorted Structure
2+
3+
import bisect
4+
5+
class Solution:
6+
def kEmptySlots(self, bulbs: List[int], k: int) -> int:
7+
active = []
8+
9+
for day, bulb in enumerate(bulbs, 1):
10+
i = bisect.bisect(active, bulb)
11+
for neighbor in active[i - (i > 0) : i + 1]:
12+
if abs(neighbor - bulb) - 1 == k:
13+
return day
14+
active.insert(i, bulb)
15+
16+
return -1

0 commit comments

Comments
 (0)