Skip to content

Commit 68b7017

Browse files
committed
Nov 30
1 parent 2c15e96 commit 68b7017

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

2024-11-November-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
| November 27 | [3243. Shortest Distance After Road Addition Queries I](https://leetcode.com/problems/shortest-distance-after-road-addition-queries-i/) | Medium | Solved |
3333
| November 28 | [2290. Minimum Obstacle Removal to Reach Corner](https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/) | Hard | Unsolved |
3434
| November 29 | [2577. Minimum Time to Visit a Cell In a Grid](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | Hard | Unsolved |
35-
| November 30 | []() | | |
35+
| November 30 | [2097. Valid Arrangement of Pairs](https://leetcode.com/problems/valid-arrangement-of-pairs/) | Hard | Unsolved |
3636

3737
## Summary
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 4 | 4 | 0 |
4141
| Medium | 21 | 12 | 9 |
42-
| Hard | 4 | 1 | 3 |
42+
| Hard | 5 | 1 | 4 |
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
def validArrangement(self, pairs: List[List[int]]) -> List[List[int]]:
7+
graph = defaultdict(list)
8+
in_degree, out_degree = defaultdict(int), defaultdict(int)
9+
for u, v in pairs:
10+
graph[u].append(v)
11+
out_degree[u] += 1
12+
in_degree[v] += 1
13+
14+
start = pairs[0][0]
15+
for node in graph:
16+
if out_degree[node] > in_degree[node]:
17+
start = node
18+
break
19+
20+
result = []
21+
stack = [start]
22+
while stack:
23+
node = stack[-1]
24+
if graph[node]:
25+
neighbour = graph[node].pop()
26+
stack.append(neighbour)
27+
else:
28+
stack.pop()
29+
if stack:
30+
result.append([stack[-1], node])
31+
result.reverse()
32+
return result
33+
34+
35+
def main():
36+
pairs = [[5, 1], [4, 5], [11, 9], [9, 4]]
37+
assert Solution().validArrangement(pairs) == [[11, 9], [9, 4],
38+
[4, 5], [5, 1]]
39+
40+
pairs = [[1, 3], [3, 2], [2, 1]]
41+
assert Solution().validArrangement(pairs) == [[1, 3], [3, 2], [2, 1]]
42+
43+
pairs = [[1, 2], [1, 3], [2, 1]]
44+
assert Solution().validArrangement(pairs) == [[1, 2], [2, 1], [1, 3]]
45+
46+
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)