20250427 finished.
This commit is contained in:
parent
ffa2553a37
commit
443c8fff12
|
@ -621,3 +621,5 @@ 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;
|
mod p2444_count_subarrays_with_fixed_bounds;
|
||||||
|
|
||||||
|
mod p3392_count_subarrays_of_length_three_with_a_condition;
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/**
|
||||||
|
* [3392] Count Subarrays of Length Three With a Condition
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn count_subarrays(nums: Vec<i32>) -> i32 {
|
||||||
|
nums.iter()
|
||||||
|
.enumerate()
|
||||||
|
.skip(2)
|
||||||
|
.filter_map(|(i, &v)| {
|
||||||
|
if (nums[i - 2] + v) * 2 == nums[i - 1] {
|
||||||
|
Some(())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.count() as i32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_3392() {
|
||||||
|
assert_eq!(1, Solution::count_subarrays(vec![1, 2, 1, 4, 1]));
|
||||||
|
assert_eq!(0, Solution::count_subarrays(vec![1, 1, 1]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user