1707. 与数组中元素的最大异或值
作者:互联网
题目来源:1707. 与数组中元素的最大异或值
给你一个由非负整数组成的数组 nums 。另有一个查询数组 queries ,其中 queries[i] = [xi, mi] 。
第 i 个查询的答案是 xi 和任何 nums 数组中不超过 mi 的元素按位异或(XOR)得到的最大值。换句话说,
答案是 max(nums[j] XOR xi) ,其中所有 j 均满足 nums[j] <= mi 。如果 nums 中的所有元素都大于 mi,最终答案就是 -1 。
返回一个整数数组 answer 作为查询的答案,其中 answer.length == queries.length 且 answer[i] 是第 i 个查询的答案。
/** 暴力法:超时了 * @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标签:xi,XOR,nums,1707,length,异或,let,数组,queries 来源: https://blog.51cto.com/u_15201483/2823656