992. Subarrays with K Different Integers Hard
1/**
2 * [992] Subarrays with K Different Integers
3 *
4 * Given an integer array nums and an integer k, return the number of good subarrays of nums.
5 * A good array is an array where the number of different integers in that array is exactly k.
6 *
7 * For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.
8 *
9 * A subarray is a contiguous part of an array.
10 *
11 * Example 1:
12 *
13 * Input: nums = [1,2,1,2,3], k = 2
14 * Output: 7
15 * Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
16 *
17 * Example 2:
18 *
19 * Input: nums = [1,2,1,3,4], k = 3
20 * Output: 3
21 * Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
22 *
23 *
24 * Constraints:
25 *
26 * 1 <= nums.length <= 2 * 10^4
27 * 1 <= nums[i], k <= nums.length
28 *
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/subarrays-with-k-different-integers/
33// discuss: https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38 pub fn subarrays_with_k_distinct(nums: Vec<i32>, k: i32) -> i32 {
39 0
40 }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn test_992() {
51 }
52}
53
Back
© 2025 bowen.ge All Rights Reserved.