Class Solution
- java.lang.Object
-
- g1901_2000.s1938_maximum_genetic_difference_query.Solution
-
public class Solution extends Object
1938 - Maximum Genetic Difference Query.Hard
There is a rooted tree consisting of
nnodes numbered0ton - 1. Each node’s number denotes its unique genetic value (i.e. the genetic value of nodexisx). 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 nodexis the root of the tree, thenparents[x] == -1.You are also given the array
querieswherequeries[i] = [nodei, vali]. For each queryi, find the maximum genetic difference betweenvaliandpi, wherepiis the genetic value of any node that is on the path betweennodeiand the root (includingnodeiand the root). More formally, you want to maximizevali XOR pi.Return an array
answhereans[i]is the answer to theithquery.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:
2 <= parents.length <= 1050 <= parents[i] <= parents.length - 1for every nodeithat is not the root.parents[root] == -11 <= queries.length <= 3 * 1040 <= nodei <= parents.length - 10 <= vali <= 2 * 105
-
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]maxGeneticDifference(int[] parents, int[][] queries)
-