1852. Distinct Numbers in Each Subarray

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

On This Page

LeetCode problem 1852

class Solution:
    def distinctNumbers(self, nums: List[int], k: int) -> List[int]:
        cnt = Counter(nums[:k])
        res = [len(cnt)]
        for i in range(k, len(nums)):
            cnt[nums[i]] += 1
            cnt[nums[i - k]] -= 1
            if cnt[nums[i - k]] == 0:
                cnt.pop(nums[i - k])
            res.append(len(cnt))
        return res