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

Commit ec43b87

Browse files
committed
Add solution #421
1 parent d4eb941 commit ec43b87

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 421. Maximum XOR of Two Numbers in an Array
3+
* https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, return the maximum result of nums[i] XOR
7+
* nums[j], where 0 <= i <= j < n.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var findMaximumXOR = function(nums) {
15+
let result = 0;
16+
17+
for (let i = 31, mask = 0; i >= 0; i--) {
18+
mask |= (1 << i);
19+
const set = new Set(nums.map(num => num & mask));
20+
const target = result | (1 << i);
21+
for (const prefix of set) {
22+
if (set.has(prefix ^ target)) {
23+
result = target;
24+
break;
25+
}
26+
}
27+
}
28+
29+
return result;
30+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@
336336
417|[Pacific Atlantic Water Flow](./0417-pacific-atlantic-water-flow.js)|Medium|
337337
419|[Battleships in a Board](./0419-battleships-in-a-board.js)|Medium|
338338
420|[Strong Password Checker](./0420-strong-password-checker.js)|Hard|
339+
421|[Maximum XOR of Two Numbers in an Array](./0421-maximum-xor-of-two-numbers-in-an-array.js)|Medium|
339340
434|[Number of Segments in a String](./0434-number-of-segments-in-a-string.js)|Easy|
340341
435|[Non-overlapping Intervals](./0435-non-overlapping-intervals.js)|Medium|
341342
437|[Path Sum III](./0437-path-sum-iii.js)|Medium|

0 commit comments

Comments
 (0)