1562. Find Latest Group of Size M
On This Page
class Solution:
def findLatestStep(self, arr: List[int], m: int) -> int:
n = len(arr)
if m == n:
return n
cnt = [0] * (n + 2)
res = -1
for i, v in enumerate(arr):
v -= 1
l, r = cnt[v - 1], cnt[v + 1]
if l == m or r == m:
res = i
cnt[v - l] = cnt[v + r] = l + r + 1
return res