Skip to main content

Island Perimeter - LeetCode Daily Challenge

Prerna Sharma

Problem Statement

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.

Try here before watching the video.

Video Solution

Java Code

class Solution {
    public int islandPerimeter(int[][] grid) {
        int neighbours = 0, cells = 0;
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == 1) {
                    cells += 1;
                    if(j - 1 >= 0 && grid[i][j - 1] == 1) neighbours += 1;
                    if(i - 1 >= 0 && grid[i - 1][j] == 1) neighbours += 1;
                }
            }
        }
        return 4 * cells - 2 * neighbours;
    }
}

C++ Code

class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int neighbours = 0, cells = 0;
        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                if (grid[i][j] == 1) {
                    cells += 1;
                    if (j - 1 >= 0 && grid[i][j - 1] == 1) neighbours += 1;
                    if (i - 1 >= 0 && grid[i - 1][j] == 1) neighbours += 1;
                }
            }
        }
        return 4 * cells - 2 * neighbours;
    }
};

Python Code

class Solution:
    def islandPerimeter(self, grid):
        neighbours = 0
        cells = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == 1:
                    cells += 1
                    if j - 1 >= 0 and grid[i][j - 1] == 1:
                        neighbours += 1
                    if i - 1 >= 0 and grid[i - 1][j] == 1:
                        neighbours += 1
        return 4 * cells - 2 * neighbours

Javascript Code

var islandPerimeter = function(grid) {
    let neighbours = 0;
    let cells = 0;
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            if (grid[i][j] === 1) {
                cells += 1;
                if (j - 1 >= 0 && grid[i][j - 1] === 1) neighbours += 1;
                if (i - 1 >= 0 && grid[i - 1][j] === 1) neighbours += 1;
            }
        }
    }
    return 4 * cells - 2 * neighbours;
};

Go Code

func islandPerimeter(grid [][]int) int {
    neighbours := 0
    cells := 0
    for i := 0; i < len(grid); i++ {
        for j := 0; j < len(grid[0]); j++ {
            if grid[i][j] == 1 {
                cells++
                if j-1 >= 0 && grid[i][j-1] == 1 {
                    neighbours++
                }
                if i-1 >= 0 && grid[i-1][j] == 1 {
                    neighbours++
                }
            }
        }
    }
    return 4*cells - 2*neighbours
}

Complexity Analysis

Time Complexity: O(N^2)
Space Complexity: O(1)