-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1743.py
38 lines (31 loc) · 1.15 KB
/
m1743.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
mappings = defaultdict(List[int])
starters = set()
for pair in adjacentPairs :
if pair[0] in mappings :
mappings[pair[0]].append(pair[1])
else :
mappings[pair[0]] = [pair[1]]
if pair[1] in mappings :
mappings[pair[1]].append(pair[0])
else :
mappings[pair[1]] = [pair[0]]
if pair[0] in starters :
starters.remove(pair[0])
else :
starters.add(pair[0])
if pair[1] in starters :
starters.remove(pair[1])
else :
starters.add(pair[1])
output = [list(starters)[0]]
while len(output) <= len(adjacentPairs) :
options = mappings[output[-1]]
if len(options) <= 1 :
output.append(options[0])
elif output[-2] != options[0] :
output.append(options[0])
else :
output.append(options[1])
return output