Skip to content

Commit 40cc97c

Browse files
committed
Add problem 3131: Find the Integer Added to Array I
1 parent 15a9582 commit 40cc97c

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,7 @@ pub mod problem_3115_maximum_prime_difference;
20982098
pub mod problem_3120_count_the_number_of_special_characters_i;
20992099
pub mod problem_3121_count_the_number_of_special_characters_ii;
21002100
pub mod problem_3127_make_a_square_with_the_same_color;
2101+
pub mod problem_3131_find_the_integer_added_to_array_i;
21012102
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21022103

21032104
#[cfg(test)]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn added_integer(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
7+
fn min(nums: &[i32]) -> i32 {
8+
nums.iter().copied().fold(i32::MAX, i32::min)
9+
}
10+
11+
min(&nums2) - min(&nums1)
12+
}
13+
}
14+
15+
// ------------------------------------------------------ snip ------------------------------------------------------ //
16+
17+
impl super::Solution for Solution {
18+
fn added_integer(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
19+
Self::added_integer(nums1, nums2)
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
#[test]
26+
fn test_solution() {
27+
super::super::tests::run::<super::Solution>();
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn added_integer(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, 6, 4] as &[_], &[9, 7, 5] as &[_]), 3),
14+
((&[10], &[5]), -5),
15+
((&[1, 1, 1, 1], &[1, 1, 1, 1]), 0),
16+
];
17+
18+
for ((nums1, nums2), expected) in test_cases {
19+
assert_eq!(S::added_integer(nums1.to_vec(), nums2.to_vec()), expected);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)