|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +class TrieNode { |
| 4 | + TrieNode[] children; |
| 5 | + int count; |
| 6 | + |
| 7 | + public TrieNode() { |
| 8 | + this.children = new TrieNode[2]; |
| 9 | + this.count = 0; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +class Trie { |
| 14 | + private final TrieNode root; |
| 15 | + |
| 16 | + public Trie() { |
| 17 | + this.root = new TrieNode(); |
| 18 | + } |
| 19 | + |
| 20 | + public void insert(int num) { |
| 21 | + TrieNode node = root; |
| 22 | + for (int i = 17; i >= 0; i--) { |
| 23 | + int bit = (num >> i) & 1; |
| 24 | + if (node.children[bit] == null) { |
| 25 | + node.children[bit] = new TrieNode(); |
| 26 | + } |
| 27 | + node = node.children[bit]; |
| 28 | + node.count++; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public void remove(int num) { |
| 33 | + TrieNode node = root; |
| 34 | + for (int i = 17; i >= 0; i--) { |
| 35 | + int bit = (num >> i) & 1; |
| 36 | + node = node.children[bit]; |
| 37 | + node.count--; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public int maxXOR(int num) { |
| 42 | + TrieNode node = root; |
| 43 | + int max_xor = 0; |
| 44 | + for (int i = 17; i >= 0; i--) { |
| 45 | + int bit = (num >> i) & 1; |
| 46 | + int toggled_bit = 1 - bit; |
| 47 | + if (node.children[toggled_bit] != null && node.children[toggled_bit].count > 0) { |
| 48 | + max_xor |= (1 << i); |
| 49 | + node = node.children[toggled_bit]; |
| 50 | + } else { |
| 51 | + node = node.children[bit]; |
| 52 | + } |
| 53 | + } |
| 54 | + return max_xor; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class Solution { |
| 59 | + public int[] maxGeneticDifference(int[] parents, int[][] queries) { |
| 60 | + int n = parents.length; |
| 61 | + |
| 62 | + // Build the tree as an adjacency list |
| 63 | + Map<Integer, List<Integer>> tree = new HashMap<>(); |
| 64 | + int root = -1; |
| 65 | + |
| 66 | + for (int i = 0; i < n; i++) { |
| 67 | + if (parents[i] == -1) { |
| 68 | + root = i; |
| 69 | + } else { |
| 70 | + tree.computeIfAbsent(parents[i], k -> new ArrayList<>()).add(i); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Group queries by node |
| 75 | + Map<Integer, List<int[]>> queryMap = new HashMap<>(); |
| 76 | + for (int i = 0; i < queries.length; i++) { |
| 77 | + int node = queries[i][0]; |
| 78 | + int val = queries[i][1]; |
| 79 | + queryMap.computeIfAbsent(node, k -> new ArrayList<>()).add(new int[]{val, i}); |
| 80 | + } |
| 81 | + |
| 82 | + // Result array |
| 83 | + int[] res = new int[queries.length]; |
| 84 | + |
| 85 | + // Trie to store and query the path genetic values |
| 86 | + Trie trie = new Trie(); |
| 87 | + |
| 88 | + // Depth-first search to solve the queries |
| 89 | + dfs(root, tree, queryMap, trie, res); |
| 90 | + |
| 91 | + return res; |
| 92 | + } |
| 93 | + |
| 94 | + private void dfs(int node, Map<Integer, List<Integer>> tree, Map<Integer, List<int[]>> queryMap, Trie trie, int[] res) { |
| 95 | + trie.insert(node); |
| 96 | + |
| 97 | + // Handle queries for this node |
| 98 | + if (queryMap.containsKey(node)) { |
| 99 | + for (int[] query : queryMap.get(node)) { |
| 100 | + int val = query[0]; |
| 101 | + int idx = query[1]; |
| 102 | + res[idx] = trie.maxXOR(val); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Recurse for children |
| 107 | + if (tree.containsKey(node)) { |
| 108 | + for (int child : tree.get(node)) { |
| 109 | + dfs(child, tree, queryMap, trie, res); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Remove the node after processing its subtree |
| 114 | + trie.remove(node); |
| 115 | + } |
| 116 | +} |
0 commit comments