Hello Everyone!
Day 2 of Week 6 was dedicated to Graph Problems, where each challenge felt like solving a real-world mystery. Graph problems require a structured approach, whether it’s exploring connected components or marking regions. Today’s tasks tested my traversal skills and my ability to manage grids as graphs.
How the Day Played Out
-
Number of Islands (Medium Difficulty)
- Count the number of islands in a grid where
1
represents land and0
represents water. -
The Strategy:
- Treated the grid as a graph and used Depth First Search (DFS) to explore connected components of land.
- Marked cells as visited by modifying the grid directly, reducing the need for extra memory.
-
The Fun Part:
- Watching islands “disappear” as I marked cells during DFS felt like uncovering hidden treasures one piece at a time.
- Count the number of islands in a grid where
-
Surrounded Regions (Medium Difficulty)
- Flip all
O
s that are surrounded byX
s toX
, leaving only border-connectedO
s intact. -
The Strategy:
- Used Breadth First Search (BFS) from the border
O
s, marking them as non-flippable. - Flipped the remaining unvisited
O
s toX
in a second pass.
- Used Breadth First Search (BFS) from the border
-
The Fun Part:
- The two-phase process (marking and flipping) was satisfying—it felt like creating order from chaos.
- Flip all
What Made Today Special
-
Graph Traversal in Grids:
- Both problems treated grids as graphs, which was a refreshing change from typical adjacency lists or matrices. Traversing them required visualizing connections effectively.
-
Efficient State Management:
- Modifying the grid directly to mark visited cells (instead of using additional data structures) improved efficiency and reduced space complexity.
-
Real-World Analogies:
- Number of Islands felt like mapping out continents, while Surrounded Regions mirrored controlling territories—it made the problems feel practical and engaging.
Key Takeaways
-
DFS vs. BFS:
- DFS is great for exploring connected components deeply, while BFS shines in scenarios requiring layer-by-layer exploration, like Surrounded Regions.
-
Grid Manipulation is Key:
- Modifying the grid in place for marking visited cells simplifies solutions and avoids unnecessary memory usage.
-
Two-Phase Solutions Work Well:
- For problems like Surrounded Regions, separating the marking and flipping phases keeps the logic clean and modular.
Reflections
Both problems today were fun and engaging. Number of Islands was straightforward but rewarding, as it strengthened my DFS implementation skills. Surrounded Regions was more nuanced, requiring careful handling of border-connected regions and strategic grid manipulation. Together, they emphasized the versatility of graph traversal techniques.
What’s Next?
Tomorrow, I’ll continue with Graph Problems, tackling Clone Graph, Evaluate Division, and another graph-based task. These will push me further into the realm of graph manipulation and logical reasoning.
Thanks for following along! Let’s continue solving, learning, and growing together.
Top comments (0)