rs: add «2962. Count Subarrays Where Max Element Appears at Least K Times»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-03-29 22:50:38 +01:00
parent 34f92878c2
commit 87750598a3
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,25 @@
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
}
}