2962. Count Subarrays Where Max Element Appears at Least K Times Medium
1/**
2 * [2962] Count Subarrays Where Max Element Appears at Least K Times
3 *
4 * You are given an integer array nums and a positive integer k.
5 * Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.
6 * A subarray is a contiguous sequence of elements within an array.
7 *
8 * <strong class="example">Example 1:
9 *
10 * Input: nums = [1,3,2,3,3], k = 2
11 * Output: 6
12 * Explanation: The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
13 *
14 * <strong class="example">Example 2:
15 *
16 * Input: nums = [1,4,2,1], k = 3
17 * Output: 0
18 * Explanation: No subarray contains the element 4 at least 3 times.
19 *
20 *
21 * Constraints:
22 *
23 * 1 <= nums.length <= 10^5
24 * 1 <= nums[i] <= 10^6
25 * 1 <= k <= 10^5
26 *
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/
31// discuss: https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36 pub fn count_subarrays(nums: Vec<i32>, k: i32) -> i64 {
37
38 }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_2962() {
49 }
50}
51
Back
© 2025 bowen.ge All Rights Reserved.