Island Perimeter

You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.

Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).

The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.


Example 1:

Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.

Example 2:

Input: grid = [[1]]
Output: 4

Example 3:

Input: grid = [[1,0]]
Output: 4


Constraints:

  • row == grid.length

  • col == grid[i].length

  • 1 <= row, col <= 100

  • grid[i][j] is 0 or 1.

  • There is exactly one island in grid.


My Solution

We can solve this problem with depth first search. We start at the first land cell we find. We find it’s perimeter by finding whether it’s bordered by water to the left, up, right and down. Then we check all of it’s neighbors and we recursively call our helper function on all land cells that we haven’t visited so far and add its perimeter to our total. We keep a track of the land cells that we visited with a boolean matrix. We then return the total perimeter.

Time complexity is O(m * n), where m is the number of rows in grid and n is the number of columns in grid because in the worst case, we end up visiting all cells. Space complexity is also O(m * n) for the space required by the visited matrix.

class Solution {
    public int islandPerimeter(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        boolean[][] visited = new boolean[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    return helper(grid, visited, i, j);
                }
            }
        }

        return 0;
    }

    public int helper(int[][] grid, boolean[][] visited, int i, int j) {
        int perim = 0;
        int m = grid.length;
        int n = grid[0].length;
        
        visited[i][j] = true;

        if (i == 0 || grid[i-1][j] == 0) {
            perim++;
        }

        if (j == 0 || grid[i][j-1] == 0) {
            perim++;
        }

        if (i == m-1 || grid[i+1][j] == 0) {
            perim++;
        }

        if (j == n-1 || grid[i][j+1] == 0) {
            perim++;
        }

        if (i > 0 && !visited[i-1][j] && grid[i-1][j] == 1) {
            perim += helper(grid, visited, i-1, j);
        }

        if (i < m-1 && !visited[i+1][j] && grid[i+1][j] == 1) {
            perim += helper(grid, visited, i+1, j);
        }

        if (j > 0 && !visited[i][j-1] && grid[i][j-1] == 1) {
            perim += helper(grid, visited, i, j-1);
        }

        if (j < n-1 && !visited[i][j+1] && grid[i][j+1] == 1) {
            perim += helper(grid, visited, i, j+1);
        }

        return perim;
    }
}
Previous
Previous

Number Complement

Next
Next

Hamming Distance