1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/rs/count-subarrays-where-max-element-appears-at-least-k-times.rs
2024-03-29 22:50:38 +01:00

25 lines
535 B
Rust

impl Solution {
pub fn count_subarrays(nums: Vec<i32>, mut k: i32) -> i64 {
let m = *nums.iter().max().expect("1 ≤ nums.len() ≤ 10⁵");
let mut counter: i64 = 0;
let mut i: usize = 0;
for j in 0..nums.len() {
if nums[j] == m {
k -= 1;
}
while k == 0 {
if nums[i] == m {
k += 1;
}
i += 1;
}
counter += i as i64;
}
counter
}
}