Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashwanth137 authored Nov 30, 2024
1 parent 6d83e29 commit 82b1a86
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions solution/2000-2099/2097.Valid Arrangement of Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ end<sub>1</sub> = 1 == 1 = start<sub>2</sub>
#### Python3

```python
class Solution:
def validArrangement(self, pairs):
graph = defaultdict(deque)
degree = defaultdict(int)

for u, v in pairs:
graph[u].append(v)
degree[u] += 1
degree[v] -= 1

start = pairs[0][0]
for node in graph:
if degree[node] > 0:
start = node
break

path = []

def traverse(node):
while graph[node]:
traverse(graph[node].popleft())
path.append(node)

traverse(start)
path.reverse()
return [[path[i], path[i + 1]] for i in range(len(path) - 1)]

```

Expand Down

0 comments on commit 82b1a86

Please sign in to comment.