From 879c2b775e55f3fa38507a904a496fce87646272 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 29 Apr 2025 12:12:33 +0800 Subject: [PATCH] 20250429 finished. --- src/problem/mod.rs | 3 ++ ..._count_subarrays_with_score_less_than_k.rs | 48 ++++++++++++++++++ ...re_max_element_appears_at_least_k_times.rs | 49 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/problem/p2302_count_subarrays_with_score_less_than_k.rs create mode 100644 src/problem/p2962_count_subarrays_where_max_element_appears_at_least_k_times.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 8c33fcf..b4d0100 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -623,3 +623,6 @@ mod p1287_element_appearing_more_than_25_in_sorted_array; mod p2444_count_subarrays_with_fixed_bounds; mod p3392_count_subarrays_of_length_three_with_a_condition; + +mod p2302_count_subarrays_with_score_less_than_k; +mod p2962_count_subarrays_where_max_element_appears_at_least_k_times; diff --git a/src/problem/p2302_count_subarrays_with_score_less_than_k.rs b/src/problem/p2302_count_subarrays_with_score_less_than_k.rs new file mode 100644 index 0000000..8cd2c73 --- /dev/null +++ b/src/problem/p2302_count_subarrays_with_score_less_than_k.rs @@ -0,0 +1,48 @@ +/** + * [2302] Count Subarrays With Score Less Than K + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn count_subarrays(nums: Vec, k: i64) -> i64 { + let n = nums.len(); + + let (mut result, mut total_sum) = (0, 0); + let mut left = 0; + + for right in 0..n { + total_sum += nums[right] as i64; + + while left <= right && total_sum * ((right - left + 1) as i64) >= k { + total_sum -= nums[left] as i64; + left += 1; + } + + result += (right as i64 - left as i64) + 1; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2302() { + assert_eq!( + 3, + Solution::count_subarrays( + vec![9, 5, 3, 8, 4, 7, 2, 7, 4, 5, 4, 9, 1, 4, 8, 10, 8, 4, 7], + 4 + ) + ); + assert_eq!(6, Solution::count_subarrays(vec![2, 1, 4, 3, 5], 10)); + assert_eq!(5, Solution::count_subarrays(vec![1, 1, 1], 5)); + } +} diff --git a/src/problem/p2962_count_subarrays_where_max_element_appears_at_least_k_times.rs b/src/problem/p2962_count_subarrays_where_max_element_appears_at_least_k_times.rs new file mode 100644 index 0000000..ca2503c --- /dev/null +++ b/src/problem/p2962_count_subarrays_where_max_element_appears_at_least_k_times.rs @@ -0,0 +1,49 @@ +/** + * [2962] Count Subarrays Where Max Element Appears at Least K Times + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn count_subarrays(nums: Vec, k: i32) -> i64 { + let max = *nums.iter().max().unwrap(); + let n = nums.len(); + + let mut result = 0; + let mut count = 0; + let mut left = 0; + + for right in 0..n { + count += if nums[right] == max { 1 } else { 0 }; + + if count >= k { + result += (n - right) as i64; + } + + while left <= right && count >= k { + count -= if nums[left] == max { 1 } else { 0 }; + + if count >= k { + result += (n - right) as i64; + } + left += 1; + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2962() { + assert_eq!(6, Solution::count_subarrays(vec![1, 3, 2, 3, 3], 2)); + assert_eq!(0, Solution::count_subarrays(vec![1, 4, 2, 1], 3)); + } +}