-
Notifications
You must be signed in to change notification settings - Fork 0
/
1707.py
107 lines (93 loc) · 3.12 KB
/
1707.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
import re
from typing import List
# import sys
import heapq
import collections
import math
# 暴力方法超时
# class Solution:
# def maximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]:
# nums.sort()
# index = sorted(range(len(queries)),key=lambda i:queries[i][1])
# ans = [-1]*len(queries)
# idx = 0
# for num in nums:
# for i in range(idx,len(index)):
# if queries[index[i]][1]<num:
# idx = i
# else:
# ans[index[i]] = max(ans[index[i]],num^queries[index[i]][0])
# # print(index)
# return ans
# 前缀树也有一个点过不去,我怀疑是在为难我-0-,python效率是低啊
class TrieNode:
def __init__(self):
self.s = ''
self.next = {}
self.is_end = False
class Trie:
def __init__(self):
self.root = TrieNode()
def get_root(self)->TrieNode:
return self.root
def add_str(self,new_str:str):
cur = self.root
for index,item in enumerate(new_str):
if item not in cur.next:
new_node = TrieNode()
new_node.s = new_str[:index+1]
cur.next[item] = new_node
cur = cur.next[item]
cur.is_end = True
def num_to_bin(num:int,depth:int)->str:
b = bin(num)[2:]
return '0'*(depth-len(b))+b
def bin32_to_num(s:str)->int:
ans = 0
nums = [4294967296, 2147483648, 1073741824, 536870912, 268435456, 134217728, 67108864, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]
for i in range(1,len(s)+1):
ans+=int(s[-i])*nums[-i]
return ans
def find_max(tree:Trie,num:int,depth:int):
s = num_to_bin(num,depth)
s = s[len(s)-depth:]
cur = tree.get_root()
for item in s:
if item =='1':
if '0' in cur.next:
cur = cur.next['0']
else:
cur = cur.next['1']
else:
if '1' in cur.next:
cur = cur.next['1']
else:
cur = cur.next['0']
return bin32_to_num(cur.s)^num
class Solution:
def maximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]:
nums.sort()
index = sorted(range(len(queries)),key=lambda i:queries[i][1])
depth = len(bin(nums[-1]))-2
i = 0
flag = True
ans = [0]*len(queries)
tree = Trie()
for num in nums:
while i<len(queries) and queries[index[i]][1]<num:
if flag:
ans[index[i]] = -1
else:
ans[index[i]] = find_max(tree,queries[index[i]][0],depth)
i+=1
flag = False
tree.add_str(num_to_bin(num,depth))
while i<len(queries):
ans[index[i]] = find_max(tree,queries[index[i]][0],depth)
i+=1
return ans
a = Solution()
in_para1 = [0,1,2,3,4]
in_para2 = [[3,1],[1,3],[5,6]]
resu = a.maximizeXor(in_para1,in_para2)
print(resu)