Island Perimeter — Solution in Python, Java & C++
Island Perimeter solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on arrays and matrix.
Island Perimeter is an easy coding problem on arrays and matrix.
- Difficulty: Easy
- Topics: Arrays, Matrix
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given a grid where 1 is land and 0 is water. Cells connect horizontally/vertically. Return the total perimeter of the land in the grid.
Example 1
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Example 2
Input: grid = [[1]]
Output: 4
Constraints
1 <= rows, cols <= 8grid[i][j] is 0 or 1.
Hints for Island Perimeter
Hint 1
Each land cell contributes 4, minus 2 for every land neighbor pair.
Hint 2
Only check up and left neighbors to count each shared edge once.
How to solve Island Perimeter
Every land cell is a unit square contributing 4 edges, but each edge shared with an adjacent land cell is interior and belongs to neither's perimeter — removing 2 from the total, one for each side. So count the land cells and subtract twice the number of adjacent land pairs.
Approach
- Sweep every cell of the grid.
- For each land cell, add
4to the total. - If the cell above it is land, subtract
2. - If the cell to its left is land, subtract
2. - Return the total.
Why it works
Checking only up and left is what makes each shared edge counted exactly once — every adjacent pair has a unique upper or left member, so scanning in row-major order encounters each border once and never twice. Checking all four directions would find every pair twice and would need a subtraction of 1 instead of 2 to compensate.
Complexity
- Time —
O(rows · cols) - Space —
O(1)
Pitfalls
- Check only up and left; checking all four neighbours double-counts every shared edge.
- Subtract
2per adjacent pair — the edge disappears from both cells' perimeters. - Guard the bounds before reading the neighbour on the first row and first column.
- No traversal is needed at all; this is a pure counting argument, not a flood fill.
Reference solution
Python
from typing import List
def islandPerimeter(grid: List[List[int]]) -> int:
per = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] != 1:
continue
per += 4
if i > 0 and grid[i - 1][j] == 1:
per -= 2
if j > 0 and grid[i][j - 1] == 1:
per -= 2
return perJava
public static int islandPerimeter(int[][] grid) {
int rows = grid.length, cols = grid[0].length, total = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] != 1) continue;
total += 4;
if (i > 0 && grid[i - 1][j] == 1) total -= 2;
if (j > 0 && grid[i][j - 1] == 1) total -= 2;
}
}
return total;
}C++
int islandPerimeter(vector<vector<int>>& grid) {
int rows = (int) grid.size(), cols = (int) grid[0].size(), total = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] != 1) continue;
total += 4;
if (i > 0 && grid[i - 1][j] == 1) total -= 2;
if (j > 0 && grid[i][j - 1] == 1) total -= 2;
}
}
return total;
}JavaScript
var islandPerimeter = function(grid) {
let per = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (grid[i][j] !== 1) continue;
per += 4;
if (i > 0 && grid[i - 1][j] === 1) per -= 2;
if (j > 0 && grid[i][j - 1] === 1) per -= 2;
}
}
return per;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Matrix and Grid Traversal