20240329 Finished
This commit is contained in:
parent
402a023105
commit
9e7e56f276
|
@ -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;
|
||||
mod p1997_first_day_where_you_have_been_in_all_the_rooms;
|
||||
mod p2908_minimum_sum_of_mountain_triplets_i;
|
56
src/problem/p2908_minimum_sum_of_mountain_triplets_i.rs
Normal file
56
src/problem/p2908_minimum_sum_of_mountain_triplets_i.rs
Normal 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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user