LeetCode 2973: Find Number of Coins to Place in Tree Nodes Solution
Master LeetCode problem 2973 (Find Number of Coins to Place in Tree Nodes), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2973. Find Number of Coins to Place in Tree Nodes
Problem Explanation
Explanation:
To solve this problem, we can use a depth-first search (DFS) approach to recursively calculate the number of coins to be placed at each node. We will keep track of the subtree size and the maximum product of cost values for 3 distinct nodes in each subtree.
- Define a recursive function
dfsthat takes the current nodeuand its parentparentas parameters. - Initialize the subtree size
subtreeSize[u]to 1. - For each child
vof nodeu, recursively calldfs(v, u)and updatesubtreeSize[u]by addingsubtreeSize[v]. - If
subtreeSize[u] < 3, place 1 coin at nodeu. - Otherwise, calculate the maximum product of cost values for 3 distinct nodes in the subtree of node
u. - Return the maximum product of cost values for node
u. - The result will be the number of coins placed at each node. :
Solution Code
class Solution {
int[] subtreeSize;
int[] coins;
List<Integer>[] tree;
int[] cost;
public int[] distributeCoins(int[][] edges, int[] cost) {
int n = cost.length;
this.cost = cost;
subtreeSize = new int[n];
coins = new int[n];
tree = new ArrayList[n];
for (int i = 0; i < n; i++) {
tree[i] = new ArrayList<>();
}
for (int[] edge : edges) {
tree[edge[0]].add(edge[1]);
tree[edge[1]].add(edge[0]);
}
dfs(0, -1);
return coins;
}
private int dfs(int u, int parent) {
subtreeSize[u] = 1;
for (int v : tree[u]) {
if (v == parent) continue;
subtreeSize[u] += dfs(v, u);
}
if (subtreeSize[u] < 3) {
coins[u] = 1;
return subtreeSize[u];
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
for (int v : tree[u]) {
if (v == parent) continue;
maxHeap.offer(dfs(v, u));
}
int product = 1;
for (int i = 0; i < 3; i++) {
product *= maxHeap.poll();
}
coins[u] = Math.max(0, product * cost[u]);
return subtreeSize[u];
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2973 (Find Number of Coins to Place in Tree Nodes)?
This page provides optimized solutions for LeetCode problem 2973 (Find Number of Coins to Place in Tree Nodes) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2973 (Find Number of Coins to Place in Tree Nodes)?
The time complexity for LeetCode 2973 (Find Number of Coins to Place in Tree Nodes) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2973 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2973 in Java, C++, or Python.