File tree Expand file tree Collapse file tree 1 file changed +93
-0
lines changed Expand file tree Collapse file tree 1 file changed +93
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Node {
2
+ constructor ( ) {
3
+ this . links = [ null , null ]
4
+ this . frequency = 0
5
+ }
6
+
7
+ containsKey ( bit ) {
8
+ return this . links [ bit ] !== null
9
+ }
10
+
11
+ get ( bit ) {
12
+ return this . links [ bit ]
13
+ }
14
+
15
+ put ( bit , node ) {
16
+ this . links [ bit ] = node
17
+ }
18
+ }
19
+
20
+ class Trie {
21
+ constructor ( ) {
22
+ this . root = new Node ( )
23
+ }
24
+
25
+ insert ( num ) {
26
+ let node = this . root
27
+ for ( let i = 31 ; i >= 0 ; i -- ) {
28
+ const bit = ( num >> i ) & 1
29
+ if ( ! node . containsKey ( bit ) ) {
30
+ node . put ( bit , new Node ( ) )
31
+ }
32
+ node = node . get ( bit )
33
+ node . frequency ++
34
+ }
35
+ }
36
+
37
+ getMax ( num ) {
38
+ let node = this . root
39
+ let res = 0
40
+ for ( let i = 31 ; i >= 0 ; i -- ) {
41
+ const bit = ( num >> i ) & 1
42
+ if ( node . containsKey ( 1 - bit ) && node . get ( 1 - bit ) . frequency > 0 ) {
43
+ res = res | ( 1 << i )
44
+ node = node . get ( 1 - bit )
45
+ } else {
46
+ if ( node . containsKey ( bit ) && node . get ( bit ) . frequency > 0 ) {
47
+ node = node . get ( bit )
48
+ } else {
49
+ return 0
50
+ }
51
+ }
52
+ }
53
+ return res
54
+ }
55
+
56
+ deleteKey ( num ) {
57
+ let node = this . root
58
+ for ( let i = 31 ; i >= 0 ; i -- ) {
59
+ const bit = ( num >> i ) & 1
60
+ if ( node . containsKey ( bit ) ) {
61
+ node = node . get ( bit )
62
+ node . frequency --
63
+ } else {
64
+ break
65
+ }
66
+ }
67
+ }
68
+ }
69
+ /**
70
+ * @param {number[] } nums
71
+ * @return {number }
72
+ */
73
+ function maximumStrongPairXor ( nums ) {
74
+ const n = nums . length
75
+ nums . sort ( ( a , b ) => a - b )
76
+ let maxi = 0
77
+ let j = 0
78
+ const t = new Trie ( )
79
+ t . insert ( nums [ 0 ] )
80
+ for ( let i = 1 ; i < n ; i ++ ) {
81
+ while ( j < i && nums [ i ] - nums [ j ] > Math . min ( nums [ i ] , nums [ j ] ) ) {
82
+ t . deleteKey ( nums [ j ] )
83
+ j ++
84
+ }
85
+ maxi = Math . max ( maxi , t . getMax ( nums [ i ] ) )
86
+ t . insert ( nums [ i ] )
87
+ }
88
+ return maxi
89
+ }
90
+
91
+ // another
92
+
93
+
1
94
const maximumStrongPairXor = ( nums ) => {
2
95
const A = nums
3
96
let res = 0 ;
You can’t perform that action at this time.
0 commit comments