Skip to content

Commit

Permalink
Merge pull request #702 from 0xff-dev/2673
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2673
  • Loading branch information
6boris authored Dec 18, 2023
2 parents 0c37a0b + ff082f9 commit 85dfb1d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# [2673.Make Costs of Paths Equal in a Binary Tree][title]

## Description
You are given an integer `n` representing the number of nodes in a **perfect binary tree** consisting of nodes numbered from `1` to `n`. The root of the tree is node `1` and each node `i` in the tree has two children where the left child is the node `2 * i` and the right child is `2 * i + 1`.

Each node in the tree also has a **cost** represented by a given **0-indexed** integer array `cost` of size `n` where `cost[i]` is the cost of node `i + 1`. You are allowed to **increment** the cost of **any** node by 1 **any** number of times.

Return the **minimum** number of increments you need to make the cost of paths from the root to each **leaf** node equal.

**Note:**

- A **perfect binary tree** is a tree where each node, except the leaf nodes, has exactly 2 children.
- The **cost of a path** is the sum of costs of nodes in the path.

**Example 1:**

![example1](./binaryytreeedrawio-4.png)

```
Input: n = 7, cost = [1,5,2,2,3,3,1]
Output: 6
Explanation: We can do the following increments:
- Increase the cost of node 4 one time.
- Increase the cost of node 3 three times.
- Increase the cost of node 7 two times.
Each path from the root to a leaf will have a total cost of 9.
The total increments we did is 1 + 3 + 2 = 6.
It can be shown that this is the minimum answer we can achieve.
```

**Example 2:**

![example2](./binaryytreee2drawio.png)

```
Input: n = 3, cost = [5,3,3]
Output: 0
Explanation: The two paths already have equal total costs, so no increments are needed.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/make-costs-of-paths-equal-in-a-binary-tree
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
package Solution

func Solution(x bool) bool {
func findBaseN(n int) int {
x := 0
for n > 0 {
n >>= 1
x++
}
return x
}

func Solution(n int, cost []int) int {
dist := make([]int, len(cost))
dist[0] = cost[0]
for start := 0; start < n/2; start++ {
dist[start*2+1] = dist[start] + cost[start*2+1]
dist[start*2+2] = dist[start] + cost[start*2+2]

}

maxPathSum := 0
for i := n / 2; i < n; i++ {
if dist[i] > maxPathSum {
maxPathSum = dist[i]
}
}
baseN := findBaseN(n)

ans := 0
start, end := n/2, n
for i := start; i < end; i++ {
dist[i] = maxPathSum - dist[i]
}
baseN--
start, end = start/2, start
for baseN > 0 {
for i := start; i < end; i++ {
left, right := i*2+1, i*2+2
dl := dist[left]
dr := dist[right]
minDiff := dl
add := dr - dl
if minDiff > dr {
minDiff = dr
add = dl - dr
}
ans += add
dist[i] = minDiff
}
start, end = start/2, start
baseN--
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
cost []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 7, []int{1, 5, 2, 2, 3, 3, 1}, 6},
{"TestCase2", 3, []int{5, 3, 3}, 0},
{"TestCase3", 15, []int{1, 5, 2, 2, 3, 3, 1, 1, 2, 3, 4, 5, 6, 7, 8}, 8},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.cost)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
Expand All @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 85dfb1d

Please sign in to comment.