/** 暴力法:超时了 * @param {number[]} nums * @param {number[][]} queries * @return {number[]} */ var maximizeXor = function(nums, queries) { nums.sort((a,b)=>a-b); let n = queries.length; let m = nums.length; let answer = new Array(n).fill(-1); for(let i=0;i<n;i++){ let r = queries[i][1]; let x = queries[i][0]; if(r<nums[0]){ answer[i] = -1; }else{ for(let j=0;(j<m && nums[j]<=r);j++){ answer[i] = Math.max(answer[i], (x ^ nums[j]) ); } } } return answer; }; let nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]] console.log(nums, queries, maximizeXor(nums, queries)) /** 字典树:左子树0,右子树1,及min(当前根节点记录最小值) * @param {number[]} nums * @param {number[][]} queries * @return {number[]} */ class prefixTrie{ constructor(){ this.root = {left:null,right:null,min:Number.MAX_SAFE_INTEGER}; } insert(num,k){ let node = this.root; for(let i=k-1;i>=0;i--){ let w = (num>>i)&1; if(num<node.min){ node.min = num; } if(w === 0){ if(!node.left){ node.left = {left:null,right:null,min:num}; } node = node.left; }else{ if(!node.right){ node.right = {left:null,right:null,min:num}; } node = node.right; } } } getMax(num, k,r){ let node = this.root; let max = 0; if(node.min>r){ return -1; } for(let i=k-1;i>=0;i--){ let w = (num>>i)&1; if(w === 1){ if(node.left){ node = node.left; max += 1<<i; }else{ node = node.right; } }else{ if(node.right && node.right.min<=r){ node = node.right; max += 1<<i; }else{ node = node.left; } } } return max; } } var maximizeXor = function(nums, queries) { nums.sort((a,b)=>a-b); let n = queries.length; let m = nums.length; let radix =Math.max( nums[m-1], ...queries.map((x)=>x[0])).toString(2).length; let prefixTree = new prefixTrie(); for(let j=0;j<m;j++){ prefixTree.insert(nums[j],radix); } let answer = new Array(n).fill(-1); for(let i=0;i<n;i++){ let r = queries[i][1]; let x = queries[i][0]; answer[i] = prefixTree.getMax(x,radix,r); } return answer; }; nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]] console.log(nums, queries, maximizeXor(nums, queries)) nums =[5,2,4,6,6,3] queries =[[12,4],[8,1],[6,3]] console.log(nums, queries, maximizeXor(nums, queries))
示例 1:
输入:nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]] 输出:[3,3,7] 解释: 1) 0 和 1 是仅有的两个不超过 1 的整数。0 XOR 3 = 3 而 1 XOR 3 = 2 。二者中的更大值是 3 。 2) 1 XOR 2 = 3. 3) 5 XOR 2 = 7. 示例 2: 输入:nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]] 输出:[15,-1,5] 提示: 1 <= nums.length, queries.length <= 105 queries[i].length == 2 0 <= nums[j], xi, mi <= 109