Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7a843f1

Browse files
committed
Add solution #1707
1 parent beb0594 commit 7a843f1

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,491 LeetCode solutions in JavaScript
1+
# 1,492 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1316,6 +1316,7 @@
13161316
1704|[Determine if String Halves Are Alike](./solutions/1704-determine-if-string-halves-are-alike.js)|Easy|
13171317
1705|[Maximum Number of Eaten Apples](./solutions/1705-maximum-number-of-eaten-apples.js)|Medium|
13181318
1706|[Where Will the Ball Fall](./solutions/1706-where-will-the-ball-fall.js)|Medium|
1319+
1707|[Maximum XOR With an Element From Array](./solutions/1707-maximum-xor-with-an-element-from-array.js)|Hard|
13191320
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13201321
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13211322
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* 1707. Maximum XOR With an Element From Array
3+
* https://leetcode.com/problems/maximum-xor-with-an-element-from-array/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array nums consisting of non-negative integers. You are also given a queries
7+
* array, where queries[i] = [xi, mi].
8+
*
9+
* The answer to the ith query is the maximum bitwise XOR value of xi and any element of nums that
10+
* does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that
11+
* nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1.
12+
*
13+
* Return an integer array answer where answer.length == queries.length and answer[i] is the
14+
* answer to the ith query.
15+
*/
16+
17+
/**
18+
* @param {number[]} nums
19+
* @param {number[][]} queries
20+
* @return {number[]}
21+
*/
22+
var maximizeXor = function(nums, queries) {
23+
nums.sort((a, b) => a - b);
24+
const sortedQueries = queries.map((q, i) => [q[0], q[1], i]).sort((a, b) => a[1] - b[1]);
25+
const result = new Array(queries.length).fill(-1);
26+
27+
const trie = {};
28+
let numIndex = 0;
29+
30+
for (const [x, m, queryIndex] of sortedQueries) {
31+
while (numIndex < nums.length && nums[numIndex] <= m) {
32+
let node = trie;
33+
for (let bit = 30; bit >= 0; bit--) {
34+
const bitValue = (nums[numIndex] >> bit) & 1;
35+
if (!node[bitValue]) node[bitValue] = {};
36+
node = node[bitValue];
37+
}
38+
numIndex++;
39+
}
40+
41+
if (numIndex === 0) continue;
42+
43+
let maxXor = 0;
44+
let node = trie;
45+
for (let bit = 30; bit >= 0; bit--) {
46+
const bitValue = (x >> bit) & 1;
47+
const oppositeBit = bitValue ^ 1;
48+
if (node[oppositeBit]) {
49+
maxXor |= (1 << bit);
50+
node = node[oppositeBit];
51+
} else {
52+
node = node[bitValue];
53+
}
54+
}
55+
result[queryIndex] = maxXor;
56+
}
57+
58+
return result;
59+
};

0 commit comments

Comments
 (0)