Skip to content

Commit c231c85

Browse files
committed
Add problem 3251: Find the Count of Monotonic Pairs II
1 parent c3b7086 commit c231c85

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

Notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,7 @@ Why is this solution correct?
373373
## [3250. Find the Count of Monotonic Pairs I](https://leetcode.com/problems/find-the-count-of-monotonic-pairs-i/)
374374

375375
Implement the [combinatorics solution](https://leetcode.com/problems/find-the-count-of-monotonic-pairs-i/solutions/5633883/python3-combinatorics-explanation-on-by-ye3ow/).
376+
377+
## [3251. Find the Count of Monotonic Pairs II](https://leetcode.com/problems/find-the-count-of-monotonic-pairs-ii/)
378+
379+
Implement the [combinatorics solution](https://leetcode.com/problems/find-the-count-of-monotonic-pairs-i/solutions/5633883/python3-combinatorics-explanation-on-by-ye3ow/).

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,7 @@ pub mod problem_3242_design_neighbor_sum_service;
21562156
pub mod problem_3248_snake_in_matrix;
21572157
pub mod problem_3249_count_the_number_of_good_nodes;
21582158
pub mod problem_3250_find_the_count_of_monotonic_pairs_i;
2159+
pub mod problem_3251_find_the_count_of_monotonic_pairs_ii;
21592160
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21602161

21612162
#[cfg(test)]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::mem;
6+
7+
impl Solution {
8+
pub fn count_of_pairs(nums: Vec<i32>) -> i32 {
9+
const MODULUS: u32 = 1_000_000_007;
10+
11+
let nums = nums.into_iter().map(i32::cast_unsigned).collect::<Vec<_>>();
12+
let mut iter = nums.iter();
13+
let mut prev = iter.next().copied().unwrap();
14+
let mut cache = (1..=prev + 1).collect::<Vec<_>>();
15+
let mut temp = Vec::new();
16+
17+
for &num in iter {
18+
let mut sum = 0;
19+
20+
temp.extend((0..=num).map(|split| {
21+
sum += prev
22+
.checked_sub(num - split)
23+
.map_or(0, |max_prev| cache[(split.min(max_prev) as usize).min(cache.len() - 1)]);
24+
25+
if sum >= MODULUS {
26+
sum -= MODULUS;
27+
}
28+
29+
sum
30+
}));
31+
32+
cache.clear();
33+
prev = num;
34+
mem::swap(&mut cache, &mut temp);
35+
}
36+
37+
cache.last().unwrap().cast_signed()
38+
}
39+
}
40+
41+
// ------------------------------------------------------ snip ------------------------------------------------------ //
42+
43+
impl super::Solution for Solution {
44+
fn count_of_pairs(nums: Vec<i32>) -> i32 {
45+
Self::count_of_pairs(nums)
46+
}
47+
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
#[test]
52+
fn test_solution() {
53+
super::super::tests::run::<super::Solution>();
54+
}
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod dynamic_programming;
2+
3+
pub trait Solution {
4+
fn count_of_pairs(nums: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[2, 3, 2] as &[_], 4), (&[5, 5, 5, 5], 126)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::count_of_pairs(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)