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:
parent
34f92878c2
commit
87750598a3
1 changed files with 25 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue