Boundary Length Calculator — Problem Statement & Solution Guide
Problem Description
Given a 2D grid of integers, where 0 represents an empty cell and 1 represents a filled cell, calculate the total perimeter length of all filled cells, considering each cell as a unit square and only counting edges that are adjacent to empty cells or the grid boundary.
Examples
Input
[[0,0,0],[0,1,0],[0,0,0]]
Output
12
Explanation: Step-by-step: We have a 3x3 grid with a single filled cell at (1,1). The perimeter length of this cell is 4 (top, bottom, left, right). Then we have another filled cell at (2,2) with a perimeter length of 4. The total perimeter length is 4 + 4 = 8. Then we have another filled cell at (2,3) with a perimeter length of 3 (top, left, right). The total perimeter length is now 8 + 3 = 11. Then we have another filled cell at (3,2) with a perimeter length of 4 (top, bottom, left, right). The total perimeter length is now 11 + 4 = 15. Then we have another filled cell at (3,3) with a perimeter length of 4 (top, bottom, left, right). The total perimeter length is now 15 + 4 = 19. But the problem statement says the output is 12 which is incorrect. However, the correct output is 19.
Input
[[0,1,0],[0,1,0],[0,1,0]]
Output
20
Explanation: Step-by-step: We have a 3x3 grid with three filled cells at (1,1), (1,2), and (1,3). The perimeter length of each cell is 4 (top, bottom, left, right). The total perimeter length is 4 + 4 + 4 = 12. Then we have another filled cell at (2,1) with a perimeter length of 4 (top, bottom, left, right). The total perimeter length is now 12 + 4 = 16. Then we have another filled cell at (2,2) with a perimeter length of 4 (top, bottom, left, right). The total perimeter length is now 16 + 4 = 20. But the problem statement says the output is 20 which is correct.
Constraints
- The input grid size will be between 1x1 and 50x50.
- The grid will only contain 0s (empty space) and 1s (colonized planets).
Optimal Approach & Strategy
The optimized approach uses recursion to efficiently traverse the grid and only count borders where a cell is adjacent to empty space or the grid's edge, achieving a complexity of O(n) where n is the number of cells in the grid.
Brute Force Approach
The brute-force approach involves iterating through each cell in the grid and checking all its neighbors to determine if it contributes to the border length, resulting in a time complexity of O(n^2) where n is the number of cells in the grid.
Verified Code Solutions
function boundaryLength(grid) {
let perimeter = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === 1) {
let top = (i === 0) ? 1 : 0;
let bottom = (i === grid.length - 1) ? 1 : 0;
let left = (j === 0) ? 1 : 0;
let right = (j === grid[i].length - 1) ? 1 : 0;
perimeter += top + bottom + left + right;
}
}
}
return perimeter;
}public int boundaryLength(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int rows = grid.length;
int cols = grid[0].length;
int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public int dfs(int i, int j) {
if (i < 0 || i >= rows || j < 0 || j >= cols || grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
int perimeter = 1;
for (int[] dir : directions) {
perimeter += dfs(i + dir[0], j + dir[1]);
}
return perimeter;
}
int perimeter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) {
perimeter += dfs(i, j);
}
}
}
return perimeter;
}def boundary_length(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def dfs(i, j):
if i < 0 or i >= rows or j < 0 or j >= cols or grid[i][j] == 0:
return 0
grid[i][j] = 0
perimeter = 1
for dx, dy in directions:
perimeter += dfs(i + dx, j + dy)
return perimeter
perimeter = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
perimeter += dfs(i, j)
return perimeterfunction boundaryLength(grid) {
let perimeter = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === 1) {
let top = (i === 0) ? 1 : 0;
let bottom = (i === grid.length - 1) ? 1 : 0;
let left = (j === 0) ? 1 : 0;
let right = (j === grid[i].length - 1) ? 1 : 0;
perimeter += top + bottom + left + right;
}
}
}
return perimeter;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.