File tree 1 file changed +6
-6
lines changed
scripts/algorithms/R/Restore the Array From Adjacent Pairs
1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime : 2248 ms (Top 15.65 % ) | Memory : 166 MB (Top 35.87 % )
1
2
class Solution :
2
3
def restoreArray (self , adjacentPairs : List [List [int ]]) -> List [int ]:
3
- # create the map
4
+ # create the map
4
5
adj = collections .defaultdict (list )
5
6
for a , b in adjacentPairs :
6
7
adj [a ].append (b )
7
8
adj [b ].append (a )
8
9
9
- # find the start num
10
+ # find the start num
10
11
start = adjacentPairs [0 ][0 ]
11
12
for k , v in adj .items ():
12
13
if len (v ) == 1 :
13
14
start = k
14
15
break
15
-
16
- # dfs to connect the graph
16
+
17
+ # dfs to connect the graph
17
18
nums = []
18
19
seen = set ()
19
20
def dfs (num ):
20
21
seen .add (num )
21
22
for next_num in adj [num ]:
22
23
if next_num in seen : continue
23
24
dfs (next_num )
24
- nums .append (num )
25
+ nums .append (num )
25
26
dfs (start )
26
27
return nums
27
-
You can’t perform that action at this time.
0 commit comments