-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathL5MaxNumTrieJava
73 lines (63 loc) · 1.55 KB
/
L5MaxNumTrieJava
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.ArrayList;
class Node {
Node links[] = new Node[2];
public Node() {
}
boolean containsKey(int ind) {
return (links[ind] != null);
}
Node get(int ind) {
return links[ind];
}
void put(int ind, Node node) {
links[ind] = node;
}
};
class Trie {
private static Node root;
//Initialize your data structure here
Trie() {
root = new Node();
}
//Inserts a word into the trie
public static void insert(int num) {
Node node = root;
for(int i = 31;i>=0;i--) {
int bit = (num >> i) & 1;
if(!node.containsKey(bit)) {
node.put(bit, new Node());
}
node = node.get(bit);
}
}
public int getMax(int num) {
Node node = root;
int maxNum = 0;
for(int i = 31;i>=0;i--) {
int bit = (num >> i) & 1;
if(node.containsKey(1 - bit)) {
maxNum = maxNum | (1<<i);
node = node.get( 1 - bit);
}
else {
node = node.get(bit);
}
}
return maxNum;
}
};
public class Solution
{
public static int maxXOR(int n, int m, ArrayList<Integer> arr1, ArrayList<Integer> arr2)
{
Trie trie = new Trie();
for(int i = 0;i<n;i++) {
trie.insert(arr1.get(i));
}
int maxi = 0;
for(int i = 0;i<m;i++) {
maxi = Math.max(maxi, trie.getMax(arr2.get(i)));
}
return maxi;
}
}