Skip to content

Commit 8bf648a

Browse files
committed
1938. Maximum Genetic Difference Query: RETRY
1 parent 2935fa7 commit 8bf648a

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Diff for: src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1461,3 +1461,4 @@ mod s1932_merge_bsts_to_create_single_bst;
14611461
mod s1935_maximum_number_of_words_you_can_type;
14621462
mod s1936_add_minimum_number_of_rungs;
14631463
mod s1937_maximum_number_of_points_with_cost;
1464+
mod s1938_maximum_genetic_difference_query;
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* [1938] Maximum Genetic Difference Query
3+
*
4+
* There is a rooted tree consisting of n nodes numbered 0 to n - 1. Each node's number denotes its unique genetic value (i.e. the genetic value of node x is x). The genetic difference between two genetic values is defined as the bitwise-XOR of their values. You are given the integer array parents, where parents[i] is the parent for node i. If node x is the root of the tree, then parents[x] == -1.
5+
* You are also given the array queries where queries[i] = [nodei, vali]. For each query i, find the maximum genetic difference between vali and pi, where pi is the genetic value of any node that is on the path between nodei and the root (including nodei and the root). More formally, you want to maximize vali XOR pi.
6+
* Return an array ans where ans[i] is the answer to the i^th query.
7+
*
8+
* Example 1:
9+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/c1.png" style="width: 118px; height: 163px;" />
10+
* Input: parents = [-1,0,1,1], queries = [[0,2],[3,2],[2,5]]
11+
* Output: [2,3,7]
12+
* Explanation: The queries are processed as follows:
13+
* - [0,2]: The node with the maximum genetic difference is 0, with a difference of 2 XOR 0 = 2.
14+
* - [3,2]: The node with the maximum genetic difference is 1, with a difference of 2 XOR 1 = 3.
15+
* - [2,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
16+
*
17+
* Example 2:
18+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/c2.png" style="width: 256px; height: 221px;" />
19+
* Input: parents = [3,7,-1,2,0,7,0,2], queries = [[4,6],[1,15],[0,5]]
20+
* Output: [6,14,7]
21+
* Explanation: The queries are processed as follows:
22+
* - [4,6]: The node with the maximum genetic difference is 0, with a difference of 6 XOR 0 = 6.
23+
* - [1,15]: The node with the maximum genetic difference is 1, with a difference of 15 XOR 1 = 14.
24+
* - [0,5]: The node with the maximum genetic difference is 2, with a difference of 5 XOR 2 = 7.
25+
*
26+
*
27+
* Constraints:
28+
*
29+
* 2 <= parents.length <= 10^5
30+
* 0 <= parents[i] <= parents.length - 1 for every node i that is not the root.
31+
* parents[root] == -1
32+
* 1 <= queries.length <= 3 * 10^4
33+
* 0 <= nodei <= parents.length - 1
34+
* 0 <= vali <= 2 * 10^5
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/maximum-genetic-difference-query/
40+
// discuss: https://leetcode.com/problems/maximum-genetic-difference-query/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn max_genetic_difference(parents: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
46+
vec![]
47+
}
48+
}
49+
50+
// submission codes end
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
#[ignore]
58+
fn test_1938_example_1() {
59+
let parents = vec![-1, 0, 1, 1];
60+
let queries = vec![vec![0, 2], vec![3, 2], vec![2, 5]];
61+
62+
let result = vec![2, 3, 7];
63+
64+
assert_eq!(Solution::max_genetic_difference(parents, queries), result);
65+
}
66+
67+
#[test]
68+
#[ignore]
69+
fn test_1938_example_2() {
70+
let parents = vec![3, 7, -1, 2, 0, 7, 0, 2];
71+
let queries = vec![vec![4, 6], vec![1, 15], vec![0, 5]];
72+
73+
let result = vec![6, 14, 7];
74+
75+
assert_eq!(Solution::max_genetic_difference(parents, queries), result);
76+
}
77+
}

0 commit comments

Comments
 (0)