Skip to content

Commit dbc9623

Browse files
authored
Create find-latest-group-of-size-m.py
1 parent 0105a46 commit dbc9623

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/find-latest-group-of-size-m.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def findLatestStep(self, arr, m):
6+
"""
7+
:type arr: List[int]
8+
:type m: int
9+
:rtype: int
10+
"""
11+
if m == len(arr):
12+
return m
13+
length = [0]*(len(arr)+2)
14+
result = -1
15+
for i, x in enumerate(arr):
16+
left, right = length[x-1], length[x+1]
17+
if left == m or right == m:
18+
result = i
19+
length[x-left] = length[x+right] = left+right+1
20+
return result

0 commit comments

Comments
 (0)