Day 95: Solving Number of Enclaves with BFS on Grid

Day 95 of my #100DaysOfCode journey 🚀 Today I solved the Number of Enclaves problem using BFS on a grid. Problem intuition: We are given a grid of land (1) and water (0), and we need to count how many land cells are part of an enclave — meaning they cannot reach the boundary of the grid. Key idea: If a land cell is connected to the boundary, it is not an enclave. Approach: • Push all boundary land cells (1) into a queue • Use BFS to mark all land connected to the boundary as visited / water • After traversal, count the remaining land cells • Those remaining cells are the enclaves Concepts reinforced: • BFS on 2D grid • Boundary traversal pattern • Connected components • Grid-to-graph conversion Time Complexity: • O(n × m) → every cell is processed at most once This problem is another strong reminder that many “grid problems” are really just graph traversal problems in disguise. The more I practice these patterns, the more naturally the solutions start forming. 🌱 #100DaysOfCode #DSA #Graphs #BFS #GridProblems #Algorithms #Python #CodingJourney #ProblemSolving #SoftwareEngineering

  • text

To view or add a comment, sign in

Explore content categories