-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountPairsWithXORinaRange.cpp
More file actions
77 lines (67 loc) · 1.73 KB
/
countPairsWithXORinaRange.cpp
File metadata and controls
77 lines (67 loc) · 1.73 KB
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
// Source: https://leetcode.com/problems/count-pairs-with-xor-in-a-range/
// Author: Miao Zhang
// Date: 2021-06-09
const int HEIGHT = 14;
class TrieNode {
public:
TrieNode() : cnt(0), children(2, nullptr) {
}
~TrieNode() {
for (TrieNode* ch: children) {
if (ch) delete ch;
}
}
public:
vector<TrieNode*> children;
// TrieNode* children[2];
int cnt;
};
class Trie {
public:
Trie() : root_(new TrieNode()) {
}
void insert(int num) {
TrieNode* p = root_.get();
for (int j = HEIGHT; j >= 0; j--) {
int index = (num >> j) & 1;
if (p->children[index] == nullptr) {
p->children[index] = new TrieNode();
}
p = p->children[index];
p->cnt++;
}
}
int getCount(int num, int limit) {
TrieNode* p = root_.get();
int cnt = 0;
for (int j = HEIGHT; j >= 0; j--) {
int bitnum = (num >> j) & 1;
int bitlimit = (limit >> j) & 1;
if (bitlimit == 1) {
if (p->children[bitnum] != nullptr) {
cnt += p->children[bitnum]->cnt;
}
p = p->children[1 - bitnum];
}
else {
p = p->children[bitnum];
}
if (p == nullptr) break;
}
return cnt;
}
private:
std::unique_ptr<TrieNode> root_;
};
class Solution {
public:
int countPairs(vector<int>& nums, int low, int high) {
Trie* trie = new Trie();
int res = 0;
for (int num: nums) {
res += trie->getCount(num, high + 1) - trie->getCount(num, low);
trie->insert(num);
}
return res;
}
};