From f971f358c139082f95c525bd79c3d9f860fec055 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 3 Sep 2024 11:33:56 +0800 Subject: [PATCH] 20240903 finished. --- src/problem/mod.rs | 3 +- .../p2708_maximum_strength_of_a_group.rs | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2708_maximum_strength_of_a_group.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 2d6cbae..3945276 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -223,4 +223,5 @@ mod p3142_check_if_grid_satisfies_conditions; mod p3153_sum_of_digit_differences_of_all_pairs; mod p3127_make_a_square_with_the_same_color; mod p1450_number_of_students_doing_homework_at_a_given_time; -mod p2024_maximize_the_confusion_of_an_exam; \ No newline at end of file +mod p2024_maximize_the_confusion_of_an_exam; +mod p2708_maximum_strength_of_a_group; \ No newline at end of file diff --git a/src/problem/p2708_maximum_strength_of_a_group.rs b/src/problem/p2708_maximum_strength_of_a_group.rs new file mode 100644 index 0000000..71e2107 --- /dev/null +++ b/src/problem/p2708_maximum_strength_of_a_group.rs @@ -0,0 +1,62 @@ +/** + * [2708] Maximum Strength of a Group + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn max_strength(nums: Vec) -> i64 { + if nums.len() == 1 { + return nums[0] as i64; + } + + let mut nums = nums; + nums.sort(); + + let mut now = 0; + let mut result = 1; + let mut found = false; + + while now < nums.len() { + if nums[now] > 0 { + break; + } + + if now + 1 < nums.len() && nums[now] * nums[now + 1] > 0 { + let r = nums[now] * nums[now + 1]; + result *= r as i64; + found = true; + now += 2; + } else { + now += 1; + } + } + + while now < nums.len() { + result *= nums[now] as i64; + found = true; + + now += 1; + } + + match found { + true => result, + false => 0 + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2708() { + assert_eq!(20, Solution::max_strength(vec![-4, -5, -4])); + assert_eq!(1350, Solution::max_strength(vec![3, -1, -5, 2, 5, -9])); + } +}