20250429 finished.
This commit is contained in:
parent
443c8fff12
commit
879c2b775e
|
@ -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;
|
||||
|
|
48
src/problem/p2302_count_subarrays_with_score_less_than_k.rs
Normal file
48
src/problem/p2302_count_subarrays_with_score_less_than_k.rs
Normal file
|
@ -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<i32>, 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));
|
||||
}
|
||||
}
|
|
@ -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<i32>, 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user