Google online challenge:-
Given a number T, the number of test cases and each test case consist of the following:
A number N, number of nodes of the undirected tree and an array VALUES which contains the value of each node. Another array of size N-1 which contains pairs, each pair has 2 numbers (v,u) which denote the edge between node v and u.
Find the maximum path sum between any 2 nodes.
1 --> T
5 --> N
[ 3, -1, -4, 5, 7 ] --> VALUES ( value of node 1 is 3 and node 2 is -1 and so on. )
[ [1, 2],
[1, 3],
[2, 4],
[2, 5] ] --> array of edges
Ans is 11 sum of node values on path from 4 to 5 i.e. value of node 4(5) + value of node 2(-1) + value of node 5(7).
I attempted by Brute Force. Can anyone suggest a better approach? Something related to DP or other.