bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1020. Number of Enclaves

1020. Number of Enclaves

Leetcode Solutions

Depth First Search (DFS) to Find Enclosed Land Cells

  1. Initialize m and n to store the number of rows and columns in the grid.
  2. Create a 2D array visited to keep track of visited cells.
  3. Iterate over all cells on the grid's boundary. If a cell is a land cell and not visited, perform DFS from that cell.
  4. In DFS, if the current cell is out of bounds, a water cell, or visited, return. Otherwise, mark it as visited and call DFS on its neighbors.
  5. After DFS, iterate over the grid and count the number of unvisited land cells.
  6. Return the count of unvisited land cells as the result.
UML Thumbnail

Breadth First Search (BFS) to Find Enclosed Land Cells

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...