-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1743 v2.py
31 lines (24 loc) · 863 Bytes
/
m1743 v2.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
class Solution:
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
mappings = defaultdict(list)
starters = set()
# Mapping
for pair in adjacentPairs :
mappings[pair[0]].append(pair[1])
mappings[pair[1]].append(pair[0])
# Find starter
starter = -1
for k, v in mappings.items() :
if len(v) == 1 :
starter = k
break
output = [starter]
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