From 9e7e56f2768ed980451598265fc9045980b3475d Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 29 Mar 2024 12:07:15 +0800 Subject: [PATCH] 20240329 Finished --- src/problem/mod.rs | 3 +- ...2908_minimum_sum_of_mountain_triplets_i.rs | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2908_minimum_sum_of_mountain_triplets_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 365c9e3..971e2d2 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -87,4 +87,5 @@ mod p322_coin_change; mod p518_coin_change_ii; mod p2642_design_graph_with_shortest_path_calculator; mod p2580_count_ways_to_group_overlapping_ranges; -mod p1997_first_day_where_you_have_been_in_all_the_rooms; \ No newline at end of file +mod p1997_first_day_where_you_have_been_in_all_the_rooms; +mod p2908_minimum_sum_of_mountain_triplets_i; \ No newline at end of file diff --git a/src/problem/p2908_minimum_sum_of_mountain_triplets_i.rs b/src/problem/p2908_minimum_sum_of_mountain_triplets_i.rs new file mode 100644 index 0000000..301fa87 --- /dev/null +++ b/src/problem/p2908_minimum_sum_of_mountain_triplets_i.rs @@ -0,0 +1,56 @@ +/** + * [2908] Minimum Sum of Mountain Triplets I + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn minimum_sum(nums: Vec) -> i32 { + let mut result = i32::MAX; + + + for j in 1..nums.len() - 1 { + let mut first = i32::MAX; + + for i in 0..j { + if nums[i] < nums[j] { + first = first.min(nums[i]); + } + } + + let mut last = i32::MAX; + + for k in j + 1..nums.len() { + if nums[k] < nums[j] { + last = last.min(nums[k]); + } + } + + if first == i32::MAX || last == i32::MAX { + continue; + } + + result = result.min(first + nums[j] + last); + } + + return if result == i32::MAX { + -1 + } else { + result + }; + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2908() { + assert_eq!(9, Solution::minimum_sum(vec![8,6,1,5,3])); + } +}