BackmediumRecursionTCSAmazon

Boundary Length Calculator Solution

Problem Statement

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.

Example 1
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.

Example 2
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).
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Boundary Length Calculator — Problem Statement & Solution Guide

RecursionMediumMixed
TimeO(n*m)
|
SpaceO(1)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n*m)
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;
}

Asked in Top Tech Interviews

TCSAmazon

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.