Skip to content

Commit 269be3f

Browse files
authored
Initial Commit
1 parent 21671cf commit 269be3f

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

coding_2025/graphs/find_cycle.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
11
'''
22
Directed Graph
3-
'''
3+
'''
4+
5+
from collections import defaultdict
6+
7+
WHITE, GRAY, BLACK = 0, 1,2
8+
9+
def has_cycle(graph):
10+
color = defaultdict(lambda: WHITE)
11+
12+
def dfs(node):
13+
if color[node] == GRAY:
14+
return True
15+
if color[node] == BLACK:
16+
return False
17+
18+
color[node] = GRAY
19+
20+
for neighbor in graph[node]:
21+
if dfs(neighbor):
22+
return True
23+
color[node] = BLACK
24+
return False
25+
26+
for node in graph:
27+
if color[node] == WHITE:
28+
if dfs(node):
29+
return True
30+
31+
return False
32+
33+
34+
35+
36+
def has_cycle_undirected(graph):
37+
visited = set()
38+
39+
def dfs(node, parent):
40+
visited.add(node)
41+
42+
for neighbor in graph[node]:
43+
if neighbor not in visited:
44+
if dfs(neighbor, node):
45+
return True
46+
elif neighbor != parent:
47+
# Back edge found (cycle)
48+
return True
49+
50+
return False
51+
52+
for node in graph:
53+
if node not in visited:
54+
if dfs(node, None):
55+
return True
56+
57+
return False

coding_2025/meta/lc_536.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def getNumber(index: int) -> (int, int):
5757
node.right = TreeNode() # Prepare right child node
5858
stack.append(node.right) # Add right child to stack
5959

60-
index += 1 # Move to next character
60+
index += 1 # Move to next character This next character could ')'
6161

6262
# At the end, either stack has one last parent to return, or return root
6363
return stack.pop() if stack else root

coding_2025/meta/lc_54.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
4+
res = [] # This will store the elements in spiral order
5+
left, right = 0, len(matrix[0]) # Initialize left and right pointers for columns
6+
top, bottom = 0, len(matrix) # Initialize top and bottom pointers for rows
7+
8+
# Loop until the pointers cross each other
9+
while left < right and top < bottom:
10+
11+
# Traverse from left to right on the top row
12+
for i in range(left, right):
13+
res.append(matrix[top][i])
14+
top += 1 # Move the top boundary down
15+
16+
# Traverse from top to bottom on the rightmost column
17+
for i in range(top, bottom):
18+
res.append(matrix[i][right - 1])
19+
right -= 1 # Move the right boundary left
20+
21+
# Check if we are still within valid boundaries after moving top and right
22+
if not (left < right and top < bottom):
23+
break # If the matrix is fully traversed, exit the loop
24+
25+
# Traverse from right to left on the bottom row
26+
for i in range(right - 1, left - 1, -1):
27+
res.append(matrix[bottom - 1][i])
28+
bottom -= 1 # Move the bottom boundary up
29+
30+
# Traverse from bottom to top on the leftmost column
31+
for i in range(bottom - 1, top - 1, -1):
32+
res.append(matrix[i][left])
33+
left += 1 # Move the left boundary right
34+
35+
return res # Return the spiral order traversal

0 commit comments

Comments
 (0)