-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #702 from 0xff-dev/2673
Add solution and test-cases for problem 2673
- Loading branch information
Showing
5 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
leetcode/2601-2700/2673.Make-Costs-of-Paths-Equal-in-a-Binary-Tree/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
51 changes: 50 additions & 1 deletion
51
leetcode/2601-2700/2673.Make-Costs-of-Paths-Equal-in-a-Binary-Tree/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+10.6 KB
...01-2700/2673.Make-Costs-of-Paths-Equal-in-a-Binary-Tree/binaryytreee2drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23 KB
...1-2700/2673.Make-Costs-of-Paths-Equal-in-a-Binary-Tree/binaryytreeedrawio-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.