File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 336
336
417|[ Pacific Atlantic Water Flow] ( ./0417-pacific-atlantic-water-flow.js ) |Medium|
337
337
419|[ Battleships in a Board] ( ./0419-battleships-in-a-board.js ) |Medium|
338
338
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|
339
340
434|[ Number of Segments in a String] ( ./0434-number-of-segments-in-a-string.js ) |Easy|
340
341
435|[ Non-overlapping Intervals] ( ./0435-non-overlapping-intervals.js ) |Medium|
341
342
437|[ Path Sum III] ( ./0437-path-sum-iii.js ) |Medium|
You can’t perform that action at this time.
0 commit comments