Skip to content

Commit 5ac2e80

Browse files
committed
Add problem 2771: Longest Non-decreasing Subarray From Two Arrays
1 parent dff5114 commit 5ac2e80

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,7 @@ pub mod problem_2761_prime_pairs_with_target_sum;
19631963
pub mod problem_2765_longest_alternating_subarray;
19641964
pub mod problem_2766_relocate_marbles;
19651965
pub mod problem_2769_find_the_maximum_achievable_number;
1966+
pub mod problem_2771_longest_non_decreasing_subarray_from_two_arrays;
19661967

19671968
#[cfg(test)]
19681969
mod test_utilities;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::{iter, mem};
6+
7+
impl Solution {
8+
pub fn max_non_decreasing_length(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
9+
let mut prev_1 = 0;
10+
let mut length_1 = 0;
11+
let mut prev_2 = 0;
12+
let mut length_2 = 0;
13+
let mut result = 0;
14+
15+
iter::zip(nums1, nums2).for_each(|(num1, num2)| {
16+
let (mut x, mut y) = (num1 as u32, num2 as u32);
17+
18+
if y < x {
19+
mem::swap(&mut x, &mut y);
20+
}
21+
22+
let new_length_1 = if x < prev_1 {
23+
1
24+
} else {
25+
(if x < prev_2 { length_1 } else { length_2 }) + 1
26+
};
27+
28+
let new_length_2 = if y < prev_1 {
29+
1
30+
} else {
31+
(if y < prev_2 { length_1 } else { length_2 }) + 1
32+
};
33+
34+
prev_1 = x;
35+
length_1 = new_length_1;
36+
prev_2 = y;
37+
length_2 = new_length_2;
38+
result = u32::max(result, length_2);
39+
});
40+
41+
result as _
42+
}
43+
}
44+
45+
// ------------------------------------------------------ snip ------------------------------------------------------ //
46+
47+
impl super::Solution for Solution {
48+
fn max_non_decreasing_length(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
49+
Self::max_non_decreasing_length(nums1, nums2)
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
#[test]
56+
fn test_solution() {
57+
super::super::tests::run::<super::Solution>();
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn max_non_decreasing_length(nums1: Vec<i32>, nums2: 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 = [
13+
((&[2, 3, 1] as &[_], &[1, 2, 1] as &[_]), 2),
14+
((&[1, 3, 2, 1], &[2, 2, 3, 4]), 4),
15+
((&[1, 1], &[2, 2]), 2),
16+
((&[4, 2], &[10, 4]), 2),
17+
((&[3, 19, 13, 19], &[20, 18, 7, 14]), 2),
18+
];
19+
20+
for ((nums1, nums2), expected) in test_cases {
21+
assert_eq!(S::max_non_decreasing_length(nums1.to_vec(), nums2.to_vec()), expected);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)