mediumRecursionPattern: Mixed

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.

Constraints

  • The input grid size will be between 1x1 and 50x50.
  • The grid will only contain 0s (empty space) and 1s (colonized planets).
Time: O(n) Space: O(n)
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.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.