Hello Everyone!
Today was all about Graphs, where each problem felt like unraveling a web of connections. Graph problems are fascinating because they mirror real-world scenarios—whether it's navigating a grid, scheduling tasks, or finding the shortest route. Tackling them required patience, structured thinking, and an adventurous spirit.
How the Day Played Out
-
Number of Islands (Medium Difficulty)
- Count the number of distinct islands in a grid where
1
represents land and0
represents water. -
The Strategy:
- Used Depth First Search (DFS) to explore each connected component of land, marking cells as visited to avoid revisiting.
- Alternatively, Breadth First Search (BFS) worked just as well for this problem.
-
The Fun Part:
- Watching the grid transform as I marked cells during exploration felt like mapping out uncharted territory.
- Count the number of distinct islands in a grid where
-
Course Schedule (Medium Difficulty)
- Determine if it’s possible to complete all courses given their prerequisites (a directed acyclic graph problem).
-
The Strategy:
- Used topological sorting with Kahn’s algorithm to detect cycles and find a valid course order.
- Kept an indegree array to track prerequisites for each course.
-
The Fun Part:
- Visualizing the dependency graph and reducing indegrees step-by-step felt like solving a logistical puzzle.
-
Shortest Path in Binary Matrix (Medium Difficulty)
- Find the shortest path from the top-left to the bottom-right in a binary matrix.
-
The Strategy:
- Used Breadth First Search (BFS) to explore the matrix layer by layer, tracking the distance to each cell.
- Carefully handled edge cases like blocked start or end points.
-
The Fun Part:
- Traversing the grid in all 8 directions added a layer of complexity, making the solution feel like solving a real-world maze.
What Made Today Unique
-
Visual Problem Solving:
- Graph problems are inherently visual. From traversing grids to managing directed graphs, every problem felt like solving a real-world scenario.
-
Breadth vs. Depth:
- Choosing between BFS and DFS added an element of strategy to each problem. Understanding when to use which approach was a valuable learning experience.
-
Practical Applications:
- Each problem had a tangible, real-world equivalent—navigating maps, scheduling courses, or exploring islands. It made solving them even more engaging.
Key Takeaways
-
Graphs Are Everywhere:
- Understanding basic traversal techniques like BFS and DFS opens doors to solving a wide range of problems.
-
Indegree Matters in DAGs:
- For problems like Course Schedule, managing indegrees efficiently is key to finding topological orders and detecting cycles.
-
Explore All Directions in Grids:
- Problems like Shortest Path in Binary Matrix require exploring all possible paths, including diagonals, which adds a layer of complexity.
Reflections
The Number of Islands problem was a great exercise in exploring connected components, while Course Schedule pushed me to think about dependencies and cycles in directed graphs. Shortest Path in Binary Matrix was particularly exciting—it felt like solving a labyrinth while racing against time.
What’s Next?
On Monday, I’ll wrap up the week with Linked List Problems, including Reverse Linked List, Merge Two Sorted Lists, and Reorder List. These problems will test my understanding of pointers and node manipulation.
Thank you for joining me on this journey! Let’s continue exploring the fascinating world of competitive programming together.
Top comments (0)