20240124 Finished
This commit is contained in:
parent
0e5ab485d5
commit
ab32ca97e6
|
@ -26,3 +26,4 @@ mod p2788_split_strings_by_separator;
|
|||
mod p410_split_array_largest_sum;
|
||||
mod p670_maximum_swap;
|
||||
mod p2765_longest_alternating_subarray;
|
||||
mod p2865_beautiful_towers_i;
|
48
src/problem/p2865_beautiful_towers_i.rs
Normal file
48
src/problem/p2865_beautiful_towers_i.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* [2865] Beautiful Towers I
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
|
||||
// submission codes start here
|
||||
|
||||
impl Solution {
|
||||
pub fn maximum_sum_of_heights(max_heights: Vec<i32>) -> i64 {
|
||||
let mut result = 0;
|
||||
|
||||
for peek in 0..max_heights.len() {
|
||||
let mut total_height = max_heights[peek] as i64;
|
||||
let mut heights = vec![0;max_heights.len()];
|
||||
heights[peek] = max_heights[peek];
|
||||
|
||||
for i in (0..peek).rev() {
|
||||
heights[i] = max_heights[i].min(heights[i + 1]);
|
||||
total_height += heights[i] as i64;
|
||||
}
|
||||
|
||||
for i in peek + 1..max_heights.len() {
|
||||
heights[i] = max_heights[i].min(heights[i - 1]);
|
||||
total_height += heights[i] as i64;
|
||||
}
|
||||
|
||||
result = result.max(total_height);
|
||||
}
|
||||
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_2865() {
|
||||
assert_eq!(Solution::maximum_sum_of_heights(vec![5,3,4,1,1]), 13);
|
||||
assert_eq!(Solution::maximum_sum_of_heights(vec![6,5,3,9,2,6]), 22);
|
||||
assert_eq!(Solution::maximum_sum_of_heights(vec![3,2,5,5,2,3]), 18);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user