From ab32ca97e61d3a9169a3d0b8c4480fa39f8c0cdc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 24 Jan 2024 10:39:34 +0800 Subject: [PATCH] 20240124 Finished --- src/problem/mod.rs | 3 +- src/problem/p2865_beautiful_towers_i.rs | 48 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2865_beautiful_towers_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index bd6cbf4..8eeee08 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -25,4 +25,5 @@ mod p2809_minimum_time_to_make_array_sum_at_most_x; mod p2788_split_strings_by_separator; mod p410_split_array_largest_sum; mod p670_maximum_swap; -mod p2765_longest_alternating_subarray; \ No newline at end of file +mod p2765_longest_alternating_subarray; +mod p2865_beautiful_towers_i; \ No newline at end of file diff --git a/src/problem/p2865_beautiful_towers_i.rs b/src/problem/p2865_beautiful_towers_i.rs new file mode 100644 index 0000000..6868803 --- /dev/null +++ b/src/problem/p2865_beautiful_towers_i.rs @@ -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) -> 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); + } +}