diff --git a/src/problem/mod.rs b/src/problem/mod.rs index c81c3f4..55c91fa 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -57,4 +57,5 @@ mod p2583_kth_largest_sum_in_a_binary_tree; mod p2476_closest_nodes_queries_in_a_binary_search_tree; mod p938_range_sum_of_bst; mod p889_construct_binary_tree_from_preorder_and_postorder_traversal; -mod p2867_count_valid_paths_in_a_tree; \ No newline at end of file +mod p2867_count_valid_paths_in_a_tree; +mod p2673_make_costs_of_paths_equal_in_a_binary_tree; \ No newline at end of file diff --git a/src/problem/p2673_make_costs_of_paths_equal_in_a_binary_tree.rs b/src/problem/p2673_make_costs_of_paths_equal_in_a_binary_tree.rs new file mode 100644 index 0000000..3b56d78 --- /dev/null +++ b/src/problem/p2673_make_costs_of_paths_equal_in_a_binary_tree.rs @@ -0,0 +1,40 @@ +/** + * [2673] Make Costs of Paths Equal in a Binary Tree + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn min_increments(n: i32, cost: Vec) -> i32 { + let mut result = 0; + let mut cost = cost; + let mut now = (n - 2) as usize; + + loop { + result += (cost[now] - cost[now + 1]).abs(); + cost[now / 2] += cost[now].max(cost[now + 1]); + + if now <= 2 { + break; + } + + now = now - 2; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2673() { + assert_eq!(Solution::min_increments(7, vec![1,5,2,2,3,3,1]), 6); + } +}