-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathL6MaxXorQueriesJava
91 lines (84 loc) · 2.61 KB
/
L6MaxXorQueriesJava
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.ArrayList;
import java.util.*;
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 ArrayList<Integer> maxXorQueries(ArrayList<Integer> arr, ArrayList<ArrayList<Integer>> queries) {
Collections.sort(arr);
ArrayList<ArrayList<Integer>> offlineQueries = new ArrayList<ArrayList<Integer>>();
int m = queries.size();
for(int i = 0;i<m;i++) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(queries.get(i).get(1));
temp.add(queries.get(i).get(0));
temp.add(i);
offlineQueries.add(temp);
}
Collections.sort(offlineQueries, new Comparator<ArrayList<Integer>> () {
@Override
public int compare(ArrayList<Integer> a, ArrayList<Integer> b) {
return a.get(0).compareTo(b.get(0));
}
});
int ind = 0;
int n = arr.size();
Trie trie = new Trie();
ArrayList<Integer> ans = new ArrayList<Integer>(m);
for(int i = 0;i<m;i++) ans.add(-1);
for(int i = 0;i<m;i++) {
while(ind<n && arr.get(ind) <= offlineQueries.get(i).get(0)) {
trie.insert(arr.get(ind));
ind++;
}
int queryInd = offlineQueries.get(i).get(2);
if(ind!=0) ans.set(queryInd, trie.getMax(offlineQueries.get(i).get(1)));
else ans.set(queryInd, -1);
}
return ans;
}
}