Class Solution
-
- All Implemented Interfaces:
public final class Solution
1938 - Maximum Genetic Difference Query\.
Hard
There is a rooted tree consisting of
n
nodes numbered0
ton - 1
. Each node's number denotes its unique genetic value (i.e. the genetic value of nodex
isx
). The genetic difference between two genetic values is defined as the bitwise-XOR of their values. You are given the integer arrayparents
, whereparents[i]
is the parent for nodei
. If nodex
is the root of the tree, thenparents[x] == -1
.You are also given the array
queries
where <code>queriesi = node<sub>i</sub>, val<sub>i</sub></code>. For each queryi
, find the maximum genetic difference between <code>val<sub>i</sub></code> and <code>p<sub>i</sub></code>, where <code>p<sub>i</sub></code> is the genetic value of any node that is on the path between <code>node<sub>i</sub></code> and the root (including <code>node<sub>i</sub></code> and the root). More formally, you want to maximize <code>val<sub>i</sub> XOR p<sub>i</sub></code>.Return an array
ans
whereans[i]
is the answer to the <code>i<sup>th</sup></code> query.Example 1:
Input: parents = -1,0,1,1, queries = \[\[0,2],3,2,2,5]
Output: 2,3,7
Explanation: The queries are processed as follows:
0,2: The node with the maximum genetic difference is 0, with a difference of 2 XOR 0 = 2.
3,2: The node with the maximum genetic difference is 1, with a difference of 2 XOR 1 = 3.
2,5: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
Example 2:
Input: parents = 3,7,-1,2,0,7,0,2, queries = \[\[4,6],1,15,0,5]
Output: 6,14,7
Explanation: The queries are processed as follows:
4,6: The node with the maximum genetic difference is 0, with a difference of 6 XOR 0 = 6.
1,15: The node with the maximum genetic difference is 1, with a difference of 15 XOR 1 = 14.
0,5: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
Constraints:
<code>2 <= parents.length <= 10<sup>5</sup></code>
0 <= parents[i] <= parents.length - 1
for every nodei
that is not the root.parents[root] == -1
<code>1 <= queries.length <= 3 * 10<sup>4</sup></code>
<code>0 <= node<sub>i</sub><= parents.length - 1</code>
<code>0 <= val<sub>i</sub><= 2 * 10<sup>5</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntArray
maxGeneticDifference(IntArray parents, Array<IntArray> queries)
-
-
Method Detail
-
maxGeneticDifference
final IntArray maxGeneticDifference(IntArray parents, Array<IntArray> queries)
-
-
-
-