Watch 10 video solutions for Projection Area of 3D Shapes, a easy level problem involving Array, Math, Geometry. This walkthrough by Mike the Coder has 2,266 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an n x n grid where we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of the cell (i, j).
We view the projection of these cubes onto the xy, yz, and zx planes.
A projection is like a shadow, that maps our 3-dimensional figure to a 2-dimensional plane. We are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
Return the total area of all three projections.
Example 1:
Input: grid = [[1,2],[3,4]]
Output: 17
Explanation: Here are the three projections ("shadows") of the shape made with each axis-aligned plane.
Example 2:
Input: grid = [[2]] Output: 5
Example 3:
Input: grid = [[1,0],[0,2]] Output: 8
Constraints:
n == grid.length == grid[i].length1 <= n <= 500 <= grid[i][j] <= 50Problem Overview: You are given an n x n grid where each cell represents a stack of cubes. The task is to compute the total projection area when the 3D structure is projected onto the XY (top), YZ (front), and ZX (side) planes. Each projection reveals different visible surfaces based on cube heights.
Approach 1: Simulation of Projections (O(m*n) time, O(1) space)
Directly simulate what each projection would look like. The top projection (XY plane) counts how many cells contain at least one cube. Iterate through the grid and increment the area whenever grid[i][j] > 0. The front projection (YZ plane) depends on the tallest stack in each row, so compute the maximum value in every row and add them together. The side projection (ZX plane) depends on the tallest stack in each column, which you can compute by scanning column-wise while iterating the grid.
This method works because each projection reduces the 3D structure to a simple 2D visibility rule: presence for the top view, row maximum for the front view, and column maximum for the side view. A single nested loop can compute the top area and track row/column maximums simultaneously. The approach relies only on simple iteration and comparisons over a matrix, making it straightforward and efficient.
Approach 2: Pre-compute Row and Column Maximums (O(m*n) time, O(m+n) space)
Instead of calculating projections during traversal, pre-compute auxiliary arrays. First iterate through the grid and maintain two arrays: rowMax[i] for the tallest stack in each row and colMax[j] for the tallest stack in each column. At the same time, count the number of non‑zero cells for the top projection. Once traversal is complete, sum all values in rowMax to get the front projection and sum colMax for the side projection.
This separation improves clarity because each projection becomes an independent calculation derived from precomputed information. The technique is common when working with arrays and matrix traversal problems where row and column aggregates are reused. Memory usage increases slightly due to the extra arrays, but the logic becomes easier to reason about.
Recommended for interviews: The simulation approach is typically expected. It demonstrates that you understand how each geometric projection works and how to derive them using simple iteration over the grid. The row/column pre-computation variant shows stronger organization and can make the code cleaner, but both approaches run in the same optimal O(m*n) time using basic math and geometry reasoning.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation of Projections | O(m*n) | O(1) | Best general solution. Minimal memory usage and easy to compute projections during traversal. |
| Pre-compute Row and Column Maximums | O(m*n) | O(m+n) | When cleaner code is preferred or row/column aggregates are reused later. |