20240329 Finished

This commit is contained in:
jackfiled 2024-03-29 12:07:15 +08:00
parent 402a023105
commit 9e7e56f276
2 changed files with 58 additions and 1 deletions

View File

@ -88,3 +88,4 @@ 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;
mod p2908_minimum_sum_of_mountain_triplets_i;

View File

@ -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>) -> 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]));
}
}