Sliding Window At Most Problem
These problems are sliding windows but solve by 2 passes
992. Subarrays with K different Integers
def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
return self.atMost(A, K) - self.atMost(A, K-1)
def atMost(self, A, K):
i = 0
record = collections.defaultdict(int)
count = 0
n = len(A)
for j in range(n):
if record[A[j]] == 0:
K-=1
record[A[j]] += 1
while K < 0:
record[A[i]] -=1
if record[A[i]] == 0:
K+=1
i+=1
count += j-i+1
return count930. Binary Subarrays with Sum
1248. Count Number of Nice Subarrays
Last updated