20250426 finished.

This commit is contained in:
jackfiled 2025-04-26 15:22:38 +08:00
parent 338f74cb9e
commit ffa2553a37
2 changed files with 58 additions and 1 deletions

View File

@ -619,4 +619,5 @@ mod p2338_count_the_number_of_ideal_arrays;
mod p2845_count_of_interesting_subarrays;
mod p1287_element_appearing_more_than_25_in_sorted_array;
mod p1287_element_appearing_more_than_25_in_sorted_array;
mod p2444_count_subarrays_with_fixed_bounds;

View File

@ -0,0 +1,56 @@
/**
* [2444] Count Subarrays With Fixed Bounds
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn count_subarrays(nums: Vec<i32>, min_k: i32, max_k: i32) -> i64 {
let mut result = 0;
let (mut border, mut last_min, mut last_max) = (None, None, None);
for i in 0..nums.len() {
if nums[i] < min_k || nums[i] > max_k {
last_max = None;
last_min = None;
border = Some(i);
}
if nums[i] == min_k {
last_min = Some(i);
}
if nums[i] == max_k {
last_max = Some(i);
}
if let Some(last_min) = last_min {
if let Some(last_max) = last_max {
let current = if let Some(border) = border {
last_min.min(last_max) - border
} else {
last_min.min(last_max) + 1
};
result += current as i64
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2444() {
assert_eq!(2, Solution::count_subarrays(vec![1, 3, 5, 2, 7, 5], 1, 5));
assert_eq!(10, Solution::count_subarrays(vec![1, 1, 1, 1], 1, 1));
}
}