1562. Find Latest Group of Size M

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1562

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