2537. Count the Number of Good Subarrays Medium
1/**
2 * [2537] Count the Number of Good Subarrays
3 *
4 * Given an integer array nums and an integer k, return the number of good subarrays of nums.
5 * A subarray arr is good if it there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].
6 * A subarray is a contiguous non-empty sequence of elements within an array.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [1,1,1,1,1], k = 10
11 * Output: 1
12 * Explanation: The only good subarray is the array nums itself.
13 *
14 * <strong class="example">Example 2:
15 *
16 * Input: nums = [3,1,4,3,2,2,4], k = 2
17 * Output: 4
18 * Explanation: There are 4 different good subarrays:
19 * - [3,1,4,3,2,2] that has 2 pairs.
20 * - [3,1,4,3,2,2,4] that has 3 pairs.
21 * - [1,4,3,2,2,4] that has 2 pairs.
22 * - [4,3,2,2,4] that has 2 pairs.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= nums.length <= 10^5
28 * 1 <= nums[i], k <= 10^9
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/count-the-number-of-good-subarrays/
34// discuss: https://leetcode.com/problems/count-the-number-of-good-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn count_good(nums: Vec<i32>, k: i32) -> i64 {
40
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_2537() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.