Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
- def at_most_k_distinct(nums, k):
- l = r = res = 0
- memo = defaultdict(int)
- while r < len(nums):
- memo[nums[r]] += 1
- if memo[nums[r]] == 1:
- k -= 1
- r += 1
- while k < 0:
- memo[nums[l]] -= 1
- if memo[nums[l]] == 0:
- k += 1
- l += 1
- res += r - l
- return res
- return at_most_k_distinct(A, K) - at_most_k_distinct(A, K - 1)
Advertisement
Add Comment
Please, Sign In to add comment