algoadvance

Leetcode 3028. Ant on the Boundary

Problem Statement

You are given a 2D grid of size m x n where each cell represents a piece of land (denoted by ‘L’) or sea (denoted by an empty string “”). An ant starts at the top-left cell (0, 0) and can move in any of the four cardinal directions (left, right, up, down). The ant can only move to an adjacent piece of land (‘L’). Determine whether the ant can reach any piece of land on the boundary of the grid (the first row, last row, first column, or last column). Return true if the ant can reach a boundary piece of land, otherwise return false.

Clarifying Questions

  1. Is movement restricted to only land cells, or can the ant move through sea cells as well to reach other land cells?
    • The ant can only move to adjacent land cells (‘L’).
  2. What is guaranteed about the grid?
    • The grid is guaranteed to be non-empty and contains at least one ‘L’ cell.
  3. Can the ant start from the top-left cell (0, 0) on a sea cell?
    • No, the problem guarantees that the starting position (0, 0) is a land cell (‘L’).

Strategy

  1. Use Depth First Search (DFS) starting from the top-left cell (0, 0) to explore all the reachable land cells.
  2. During the DFS traversal, check for any boundary land cells.
  3. If any boundary land cell is reached, return true.
  4. If DFS completes without finding any boundary land cell, return false.

Code

public class AntOnBoundary {
    private int[][] directions = // use example from above
    
    public boolean canReachBoundary(char[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        
        boolean[][] visited = new boolean[m][n];
        
        return dfs(grid, visited, 0, 0, m, n);
    }
    
    private boolean dfs(char[][] grid, boolean[][] visited, int x, int y, int m, int n) {
        if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y] || grid[x][y] != 'L') {
            return false;
        }
        
        // If we are at a boundary and it's a land cell
        if ((x == 0 || y == 0 || x == m - 1 || y == n - 1) && grid[x][y] == 'L') {
            return true;
        }
        
        visited[x][y] = true;
        
        for (int[] direction : directions) {
            int newX = x + direction[0];
            int newY = y + direction[1];
            if (dfs(grid, visited, newX, newY, m, n)) {
                return true;
            }
        }
        
        return false;
    }
    
    public static void main(String[] args) {
        AntOnBoundary sol = new AntOnBoundary();
        char[][] grid1 = {
                {'L', 'L', 'L'},
                {'L', '', 'L'},
                {'L', 'L', 'L'}
        };
        System.out.println(sol.canReachBoundary(grid1)); // true
        
        char[][] grid2 = {
                {'L', 'L', 'L'},
                {'', '', ''},
                {'L', 'L', 'L'}
        };
        System.out.println(sol.canReachBoundary(grid2)); // true
        
        char[][] grid3 = {
                {'L', '', 'L'},
                {'', 'L', ''},
                {'L', '', 'L'}
        };
        System.out.println(sol.canReachBoundary(grid3)); // true

        char[][] grid4 = {
                {'L', '', ''},
                {'', 'L', ''},
                {'', '', 'L'}
        };
        System.out.println(sol.canReachBoundary(grid4)); // false
    }
}

Time Complexity

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI