2626 Count The Number Of Good Subarrays
2626 Count The Number Of Good Subarrays
Count the Number of Good Subarrays 
Given an integer array nums and an integer k, return the number of good subarrays of nums.
A subarray arr is good if there are **at least **k pairs of indices (i, j) such that i < j and arr[i] == arr[j].
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
1
2
3
4
5
**Input:** nums = [1,1,1,1,1], k = 10
**Output:** 1
**Explanation:** The only good subarray is the array nums itself.
Example 2:
1
2
3
4
5
6
7
8
9
**Input:** nums = [3,1,4,3,2,2,4], k = 2
**Output:** 4
**Explanation:** There are 4 different good subarrays:
- [3,1,4,3,2,2] that has 2 pairs.
- [3,1,4,3,2,2,4] that has 3 pairs.
- [1,4,3,2,2,4] that has 2 pairs.
- [4,3,2,2,4] that has 2 pairs.
Constraints:
1
2
1 <= nums.length <= 105
1 <= nums[i], k <= 109
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def countGood(self, nums: List[int], k: int) -> int:
n = len(nums)
same, right = 0, -1
cnt = Counter()
ans = 0
for left in range(n):
while same < k and right + 1 < n:
right += 1
same += cnt[nums[right]]
cnt[nums[right]] += 1
if same >= k:
ans += n - right
cnt[nums[left]] -= 1
same -= cnt[nums[left]]
return ans
This post is licensed under CC BY 4.0 by the author.