Skip to content

Commit ae8f4b2

Browse files
committed
Add problem 3698: Split Array With Minimum Difference
1 parent 5ff2b81 commit ae8f4b2

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,7 @@ pub mod problem_3688_bitwise_or_of_even_numbers_in_an_array;
23062306
pub mod problem_3689_maximum_total_subarray_value_i;
23072307
pub mod problem_3692_majority_frequency_characters;
23082308
pub mod problem_3697_compute_decimal_representation;
2309+
pub mod problem_3698_split_array_with_minimum_difference;
23092310
pub mod problem_3701_compute_alternating_sum;
23102311
pub mod problem_3707_equal_score_substrings;
23112312
pub mod problem_3708_longest_fibonacci_subarray;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn split_array(nums: Vec<i32>) -> i64 {
7+
let nums = nums.into_iter().map(i32::cast_unsigned).collect::<Vec<_>>();
8+
let mut prev_1 = 0_u64;
9+
let mut candidate = 0_u64;
10+
let mut iter = nums.iter().copied();
11+
12+
let mut prev_2 = u64::from(loop {
13+
if let Some(num) = iter.next() {
14+
if (prev_1 as u32) < num {
15+
prev_1 = u64::from(num);
16+
candidate += prev_1;
17+
} else {
18+
break num;
19+
}
20+
} else {
21+
return (candidate - prev_1).abs_diff(prev_1).cast_signed();
22+
}
23+
});
24+
25+
candidate = candidate.wrapping_sub(prev_2);
26+
27+
for num in iter {
28+
if num < prev_2 as u32 {
29+
prev_2 = u64::from(num);
30+
candidate = candidate.wrapping_sub(prev_2);
31+
} else {
32+
return -1;
33+
}
34+
}
35+
36+
let mut result = candidate.cast_signed().unsigned_abs();
37+
38+
if prev_1 != prev_2 {
39+
result = result.min(candidate.wrapping_sub(prev_1 * 2).cast_signed().unsigned_abs());
40+
}
41+
42+
result.cast_signed()
43+
}
44+
}
45+
46+
// ------------------------------------------------------ snip ------------------------------------------------------ //
47+
48+
impl super::Solution for Solution {
49+
fn split_array(nums: Vec<i32>) -> i64 {
50+
Self::split_array(nums)
51+
}
52+
}
53+
54+
#[cfg(test)]
55+
mod tests {
56+
#[test]
57+
fn test_solution() {
58+
super::super::tests::run::<super::Solution>();
59+
}
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn split_array(nums: Vec<i32>) -> i64;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
(&[1, 3, 2] as &[_], 2),
14+
(&[1, 2, 4, 3], 4),
15+
(&[3, 1, 2], -1),
16+
(&[29, 30, 28, 27, 21, 18], 35),
17+
];
18+
19+
for (nums, expected) in test_cases {
20+
assert_eq!(S::split_array(nums.to_vec()), expected);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)