File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string[][] } regions
3+ * @param {string } region1
4+ * @param {string } region2
5+ * @return {string }
6+ */
7+ const findSmallestRegion = function ( regions , region1 , region2 ) {
8+ const hash = { }
9+ for ( const arr of regions ) {
10+ const p = arr [ 0 ]
11+ const size = arr . length
12+ for ( let i = 1 ; i < size ; i ++ ) {
13+ const e = arr [ i ]
14+ if ( hash [ e ] == null ) hash [ e ] = [ ]
15+ hash [ e ] . push ( p )
16+ }
17+ }
18+
19+ const path1 = [ region1 ] , path2 = [ region2 ]
20+ traverse ( region1 , path1 )
21+ traverse ( region2 , path2 )
22+
23+ let i = path1 . length - 1 , j = path2 . length - 1
24+ while ( i >= 0 && j >= 0 ) {
25+ if ( path1 [ i ] !== path2 [ j ] ) break
26+ else {
27+ i --
28+ j --
29+ }
30+ }
31+
32+ return path1 [ i + 1 ]
33+
34+ function traverse ( node , res ) {
35+ if ( hash [ node ] == null ) return
36+ res . push ( hash [ node ] [ 0 ] )
37+ traverse ( hash [ node ] [ 0 ] , res )
38+ }
39+
40+ }
You can’t perform that action at this time.
0 commit comments