From 7ac23d9b34735fbb9fd15f2dd3ed7b0d5eefdaad Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 21 Sep 2024 14:01:33 +0800 Subject: [PATCH] Add fast path --- .../meet_in_the_middle.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/problem_2035_partition_array_into_two_arrays_to_minimize_sum_difference/meet_in_the_middle.rs b/src/problem_2035_partition_array_into_two_arrays_to_minimize_sum_difference/meet_in_the_middle.rs index bb44100d..a0b70a49 100644 --- a/src/problem_2035_partition_array_into_two_arrays_to_minimize_sum_difference/meet_in_the_middle.rs +++ b/src/problem_2035_partition_array_into_two_arrays_to_minimize_sum_difference/meet_in_the_middle.rs @@ -37,7 +37,13 @@ impl Solution { let i = right_sums.partition_point(|&x| x < target); if let Some(&right_sum) = right_sums.get(i) { - min_sum = u32::min(min_sum, ((left_sum + right_sum) * 2 - sum) as _); + let candidate = (left_sum + right_sum) * 2 - sum; + + if candidate == 0 { + return 0; + } + + min_sum = u32::min(min_sum, candidate as _); } } }