20240228 Finished
This commit is contained in:
parent
2c7a678406
commit
72ac17fde7
|
@ -58,3 +58,4 @@ 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;
|
||||
mod p2673_make_costs_of_paths_equal_in_a_binary_tree;
|
|
@ -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>) -> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user