Skip to content

Commit d012705

Browse files
authored
Create k-empty-slots.py
1 parent 9b1603a commit d012705

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/k-empty-slots.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def kEmptySlots(self, flowers, k):
6+
"""
7+
:type flowers: List[int]
8+
:type k: int
9+
:rtype: int
10+
"""
11+
days = [0] * len(flowers)
12+
for i in xrange(len(flowers)):
13+
days[flowers[i]-1] = i
14+
result = float("inf")
15+
i, left, right = 0, 0, k+1
16+
while right < len(days):
17+
if days[i] < days[left] or days[i] <= days[right]:
18+
if i == right:
19+
result = min(result, max(days[left], days[right]))
20+
left, right = i, k+1+i;
21+
i += 1
22+
return -1 if result == float("inf") else result+1
23+

0 commit comments

Comments
 (0)