463. Island Perimeter
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the perimeter of a single island in a grid where 1 represents land and 0 represents water. The grid is surrounded by water, the island is contiguous, and it has no "lakes" (internal bodies of water). The perimeter is the total length of the boundary between land and water cells, or between land cells and the grid's edge.
Solution
Island Perimeter in Python
class Solution:
def islandPerimeter(self, grid: list[list[int]]) -> int:
rows = len(grid)
# The problem constraints state 1 <= row, col <= 100, so this check is for robustness.
if rows == 0:
return 0
cols = len(grid[0])
land_cells = 0
shared_edges = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
land_cells += 1
# Check for a land cell below to count vertical shared edges.
# We only check down and right to avoid double counting.
if i + 1 < rows and grid[i+1][j] == 1:
shared_edges += 1
# Check for a land cell to the right to count horizontal shared edges.
if j + 1 < cols and grid[i][j+1] == 1:
shared_edges += 1
# The total perimeter is calculated by taking the total possible perimeter
# (each land cell * 4 sides) and subtracting the shared internal edges.
# Each shared edge removes two sides from the total perimeter count (one from each cell).
return 4 * land_cells - 2 * shared_edges
Island Perimeter in Java
class Solution {
public int islandPerimeter(int[][] grid) {
int rows = grid.length;
// The problem constraints state 1 <= row, col <= 100, so this check is for robustness.
if (rows == 0) {
return 0;
}
int cols = grid[0].length;
int landCells = 0;
int sharedEdges = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) {
landCells++;
// Count shared edge with the cell below (vertical).
// We only check down and right to avoid double counting.
if (i + 1 < rows && grid[i+1][j] == 1) {
sharedEdges++;
}
// Count shared edge with the cell to the right (horizontal).
if (j + 1 < cols && grid[i][j+1] == 1) {
sharedEdges++;
}
}
}
}
// The total perimeter is calculated by taking the total possible perimeter
// (each land cell * 4 sides) and subtracting the shared internal edges.
// Each shared edge removes two sides from the total perimeter count (one from each cell).
return 4 * landCells - 2 * sharedEdges;
}
}
Island Perimeter in C++
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int rows = grid.size();
// The problem constraints state 1 <= row, col <= 100, so this check is for robustness.
if (rows == 0) {
return 0;
}
int cols = grid[0].size();
int land_cells = 0;
int shared_edges = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (grid[i][j] == 1) {
land_cells++;
// Check for a land cell below to count vertical shared edges.
// We only check down and right to avoid double counting.
if (i + 1 < rows && grid[i+1][j] == 1) {
shared_edges++;
}
// Check for a land cell to the right to count horizontal shared edges.
if (j + 1 < cols && grid[i][j+1] == 1) {
shared_edges++;
}
}
}
}
// The total perimeter is calculated by taking the total possible perimeter
// (each land cell * 4 sides) and subtracting the shared internal edges.
// Each shared edge removes two sides from the total perimeter count (one from each cell).
return 4 * land_cells - 2 * shared_edges;
}
};
Island Perimeter in TypeScript
function islandPerimeter(grid: number[][]): number {
const rows = grid.length;
// The problem constraints state 1 <= row, col <= 100, so this check is for robustness.
if (rows === 0) {
return 0;
}
const cols = grid[0].length;
let landCells = 0;
let sharedEdges = 0;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (grid[i][j] === 1) {
landCells++;
// Count shared edge with the cell below (vertical).
// We only check down and right to avoid double counting.
if (i + 1 < rows && grid[i+1][j] === 1) {
sharedEdges++;
}
// Count shared edge with the cell to the right (horizontal).
if (j + 1 < cols && grid[i][j+1] === 1) {
sharedEdges++;
}
}
}
}
// The total perimeter is calculated by taking the total possible perimeter
// (each land cell * 4 sides) and subtracting the shared internal edges.
// Each shared edge removes two sides from the total perimeter count (one from each cell).
return 4 * landCells - 2 * sharedEdges;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.