Skip to content

Commit 5a25b5b

Browse files
committed
Added 2673
1 parent 0186fd6 commit 5a25b5b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@
695695
| 2487 | [Remove Nodes From Linked List](src/2487.remove-nodes-from-linked-list.py) | Medium | O(N) | O(N) | Linked List, Stack, Recursion, Monotonic Stack | | |
696696
| 2498 | [Frog Jump II](src/2498.frog-jump-ii.py) | Medium | O(N) | O(1) | Array, Binary Search, Greedy | | |
697697
| 2517 | [Maximum Tastiness of Candy Basket](src/2517.maximum-tastiness-of-candy-basket.py) | Medium | O(NlogN) | O(1) | Array, Binary Search, Sorting | | |
698+
| 2673 | [Make Costs of Paths Equal in a Binary Tree](src/2673.make-costs-of-paths-equal-in-a-binary-tree.py) | Medium | O(N) | O(logN) | Array, Dynamic Programming, Greedy, Tree, Binary Tree | | |
698699
| 2958 | [Length of Longest Subarray With at Most K Frequency](src/2958.length-of-longest-subarray-with-at-most-k-frequency.py) | Medium | O(N) | O(N) | Array, Hash Table, Sliding Window | | |
699700
| 2962 | [Count Subarrays Where Max Element Appears at Least K Times](src/2962.count-subarrays-where-max-element-appears-at-least-k-times.py) | Medium | O(N) | O(N) | Array, Sliding Window | | |
700701
| 2966 | [Divide Array Into Arrays With Max Difference](src/2966.divide-array-into-arrays-with-max-difference.py) | Medium | O(NlogN) | O(N) | Array, Greedy, Sorting | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# @lc app=leetcode id=2673 lang=python3
3+
#
4+
# [2673] Make Costs of Paths Equal in a Binary Tree
5+
#
6+
7+
8+
# @lc code=start
9+
# TAGS: Array, Dynamic Programming, Greedy, Tree, Binary Tree
10+
class Solution:
11+
# Time: O(N). Space: O(logN)
12+
def minIncrements(self, n: int, cost: List[int]) -> int:
13+
self.counter = 0
14+
15+
def dfs(node=1):
16+
if node > len(cost):
17+
return 0
18+
left = dfs(node * 2)
19+
right = dfs(node * 2 + 1)
20+
self.counter += abs(left - right)
21+
return max(left, right) + cost[node - 1]
22+
23+
dfs()
24+
return self.counter
25+
26+
27+
# @lc code=end

0 commit comments

Comments
 (0)