Блог пользователя aviator_012

Автор aviator_012, история, 4 года назад, По-английски

I found an interesting problem on the tree in google's online assessment. You have been given a Tree consisting of N nodes, and an array V of length N presents the value of ith Node — V[i]. You have to find the number of distinct paths of length greater than one where the value of the starting node equals the value of the ending node. And all the node values between starting and ending nodes should be less than or equal to the value of starting node.

constraints:
1<= N <= 100000

Sample input:
N = 5
edges = [{1,2}, {1,3}, {3, 4}, {3, 5}]
V = [2, 3, 1, 2, 3]

Output:
2

explanation:
path = 1-3-4
path = 2-1-3-5

I was able to come up with a solution with a time complexity of O(N^3). I was wondering how to solve this efficiently.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +31
  • Проголосовать: не нравится