We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 791cfb8 commit 8b82570Copy full SHA for 8b82570
solution/2000-2099/2097.Valid Arrangement of Pairs/README_EN.md
@@ -87,6 +87,32 @@ end<sub>1</sub> = 1 == 1 = start<sub>2</sub>
87
#### Python3
88
89
```python
90
+class Solution:
91
+ def validArrangement(self, pairs):
92
+ graph = defaultdict(deque)
93
+ degree = defaultdict(int)
94
+
95
+ for u, v in pairs:
96
+ graph[u].append(v)
97
+ degree[u] += 1
98
+ degree[v] -= 1
99
100
+ start = pairs[0][0]
101
+ for node in graph:
102
+ if degree[node] > 0:
103
+ start = node
104
+ break
105
106
+ path = []
107
108
+ def traverse(node):
109
+ while graph[node]:
110
+ traverse(graph[node].popleft())
111
+ path.append(node)
112
113
+ traverse(start)
114
+ path.reverse()
115
+ return [[path[i], path[i + 1]] for i in range(len(path) - 1)]
116
117
```
118
0 commit comments