-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-times-substring.py
More file actions
42 lines (37 loc) · 1.17 KB
/
Copy pathstring-times-substring.py
File metadata and controls
42 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def kEmptySlots(self, flowers, k):
"""
:type flowers: List[int]
:type k: int
:rtype: int
"""
positions = ([None] * len(flowers))[:]
for i in range(len(flowers)):
p = flowers[i] - 1
positions[p] = i
if p - k - 1 >= 0 and positions[p - k - 1] is not None:
r = p - 1
possible = True
while r > p - k - 1:
if positions[r] is not None:
possible = False
break
r -= 1
if possible:
return i + 1
if p + k + 1 < len(flowers) and positions[p + k + 1] is not None:
r = p + 1
possible = True
while r < p + k + 1:
if positions[r] is not None:
possible = False
break
r += 1
if possible:
return i + 1
return -1
def main():
s = Solution()
print(s.kEmptySlots([6,5,8,9,7,1,10,2,3,4],2))
if __name__ == '__main__':
main()