21. Subarrays with K Different Integers
Given an array nums
of positive integers, call a (contiguous, not necessarily distinct) subarray of nums
good if the number of different integers in that subarray is exactly k
.
(For example, [1,2,3,1,2]
has 3
different integers: 1
, 2
, and 3
.)
Return the number of good subarrays of nums
.
Example 1:
Example 2:
Solution: (Sliding Window)
Approach: Exact(k) = Atmost(k) - Atmost(k-1)
Time Complexity: O(n)
Previous20. Longest Repeating Character ReplacementNext22. Replace the Substring for Balanced String
Last updated