Tree Points
Updated
Problem statement
Given a tree of g_nodes nodes rooted at 0, each node has a value given in array A. There is also a parameter, K.
An unweighted undirected tree is represented with g_nodes nodes numbered from 0 to g_nodes - 1 and g_edges = g_nodes - 1 edges where the ith edge connects the nodes numbered g_from[i] to g_to[i]. Each node numbered u is associated with a value represented by A[u].
Collect all of the values at the nodes using one of two methods:
- Collect
A[j] - Kpoints. - Collect
floor(A[j]/2)points. If this method is chosen, all values of node j in this node's subtree are changed to thefloor(A[j]/2)as well.
You can only collect the value at a node if its parent's value has been collected before (except the root). You have to maximize the total points collected.
Note: It is possible that the accumulated value may be negative at some point.
Function
treePoints(g_nodes: int, g_from: int[], g_to: int[], A: int[], K: int) → long
Complete the function treePoints in the editor.
treePoints has the following parameters:
int g_nodes: the number of nodes in the graphint g_from[g_nodes - 1]: one end of each edgeint g_to[g_nodes - 1]: the other end each edgeint A[g_nodes]: the values at the nodesint K: the cost to collect a full value
Returns
long: the maximum sum of values that can be collected
Examples
Example 1
g_nodes = 4g_from = [0, 1, 2]g_to = [1, 2, 3]A = [10, 10, 3, 3]K = 5return = 11
The first line shows the initial tree. Nodes are labeled as <node number>:<node value>. Darker nodes are ones that change in an operation.
For the first two values, reduce them by K = 5 and collect them. When the value at node 2 is collected as floor(3/2), the value at nodes j below it are changed using the same method, floor(A[j]/2). Hence, the value at node 3 is changed to 1. The value at node 3 is then collected using the second method as floor(1/2). The total collected is 5 + 5 + 1 + 0 = 11.
Constraints
- 1 ≤
g_nodes≤ 10^5 g_edges = g_nodes - 1- 0 ≤
g_from[i],g_to[i]<g_nodes g_from[i]≠g_to[i]- 1 ≤
A[i]≤ 10^9 - 1 ≤
K≤ 10^9
More The D.E. Shaw Group interview questions
- IPOPHONE SCREEN · Seen Nov 2026
- Print Numbers in Turn with Three ThreadsPHONE SCREEN · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Koko Eating BananasPHONE SCREEN · Seen Nov 2026
- Sum of Smallest Non-DivisorsPHONE SCREEN · Seen Nov 2026
- Count Lucky TicketsOA · Seen Nov 2026
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Coordinate Odd and Even Workers in OrderONSITE INTERVIEW · Seen Oct 2026
- Minimum Taps to Water a GardenONSITE INTERVIEW · Seen Oct 2026
- Remove the Fewest Magic BeansPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Part 2: Batch Access ReviewScale AI · OAScale AI interview questions
- Binary Tree CamerasAmazon · OA · PHONE SCREEN
- Maximum Sum Path Between Two Leaf NodesGoogle · OAGoogle interview questions
- Maximum Strength of Every NeuronMicrosoft · OAMicrosoft interview questions
- Binary Tree Maximum Path SumArcesium · OAArcesium interview questions
- Tree Node With Maximum BeautyC3.ai · OAC3.ai interview questions
- Best Sum Downward Tree PathCitadel · OACitadel interview questions
- Longest Alternating a/b Path in a TreeHudson River Trading · OAHudson River Trading interview questions
- Maximum Reservoir PowerWells Fargo · OAWells Fargo interview questions