-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountPairsWithXORinaRange.py
48 lines (40 loc) · 1.23 KB
/
countPairsWithXORinaRange.py
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/count-pairs-with-xor-in-a-range/
# Author: Miao Zhang
# Date: 2021-06-09
class TrieNode:
def __init__(self):
self.children = collections.defaultdict(TrieNode)
self.cnt = 0
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, num):
p = self.root
for i in reversed(range(15)):
bitnum = (num >> i) & 1
p.children[bitnum].cnt += 1
p = p.children[bitnum]
def count(self, num, limit):
cnt = 0
p = self.root
for i in reversed(range(15)):
bitnum = (num >> i) & 1
bitlimit = (limit >> i) & 1
if bitlimit == 1:
cnt += p.children[bitnum].cnt
p = p.children[1 - bitnum]
else:
p = p.children[bitnum]
if not p:
break
return cnt
class Solution:
def countPairs(self, nums: List[int], low: int, high: int) -> int:
trie = Trie()
res = 0
for num in nums:
res += trie.count(num, high + 1) - trie.count(num, low)
trie.insert(num)
return res