-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path1803-count-pairs-with-xor-in-a-range.js
94 lines (84 loc) · 1.77 KB
/
1803-count-pairs-with-xor-in-a-range.js
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
92
93
94
/**
* @param {number[]} nums
* @param {number} low
* @param {number} high
* @return {number}
*/
const countPairs = function (nums, low, high) {
const trie = new Trie()
let ans = 0
for (let x of nums) {
ans += trie.count(x, high + 1) - trie.count(x, low)
trie.insert(x)
}
return ans
}
class Trie {
constructor() {
this.root = {}
}
insert(val) {
let node = this.root
for (let i = 14; i >= 0; i--) {
let bit = (val >> i) & 1
if (!(bit in node)) node[bit] = { cnt: 1 }
else node[bit]['cnt'] += 1
node = node[bit]
}
}
count(val, high) {
let ans = 0
let node = this.root
for (let i = 14; i >= 0; i--) {
if (!node) break
const bit = (val >> i) & 1
const cmp = (high >> i) & 1
if (cmp) {
if (node[bit]) ans += node[bit]['cnt']
node = node[1 ^ bit]
} else node = node[bit]
}
return ans
}
}
// another
/**
* @param {number[]} nums
* @param {number} low
* @param {number} high
* @return {number}
*/
const countPairs = function(nums, low, high) {
let res = 0, n = nums.length, trie = { cnt: 0 }
for(const e of nums) {
res += helper(e, high + 1) - helper(e, low)
insert(e)
}
return res
function helper(e, limit) {
let res = 0, cur = trie
for(let i = 14; i >= 0; i--) {
const a = (e >> i) & 1
const b = (limit >> i) & 1
if(cur == null) break
if(b === 0) {
cur = cur[a]
continue
}
if(cur[a]) {
res += cur[a].cnt
}
cur = cur[1 - a]
}
return res
}
function insert(e) {
let cur = trie
for(let i = 14; i >= 0; i--) {
const tmp = (e >> i) & 1
if(!(tmp in cur)) cur[tmp] = {cnt:0}
cur[tmp].cnt++
cur = cur[tmp]
}
}
};