Projection Area of 3D Shapes — Problem & Solution
Projection Area of 3D Shapes solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Projection Area of 3D Shapes is an easy coding problem on arrays, math and matrix, asked in Amazon and Google interviews.
- Difficulty: Easy
- Topics: Arrays, Math, Matrix, Geometry
- Asked at: Amazon, Google
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given an n × n grid where grid[r][c] is the height of a tower of 1 × 1 × 1 cubes standing on cell (r, c).
Return the total area of the three projections: looking straight down (the xy plane), from the front (the yz plane) and from the side (the zx plane).
Example 1
Input: grid = [[1,2],[3,4]]
Output: 17
Explanation: Top view 4, front view 3 + 4 = 7, side view 2 + 4 = 6.
Example 2
Input: grid = [[2]]
Output: 5
Explanation: 1 from above and 2 from each side.
Example 3
Input: grid = [[1,0],[0,2]]
Output: 8
Constraints
1 <= grid.length <= 8grid[i].length == grid.length0 <= grid[i][j] <= 50
Hints for Projection Area of 3D Shapes
Hint 1
The top view counts every cell with a non-zero height — one unit of area each.
Hint 2
The side view sums the maximum of each row; the front view sums the maximum of each column.
Hint 3
One nested loop can accumulate all three at once.
How to solve Projection Area of 3D Shapes
Top view counts non-zero cells; side view sums row maxima; front view sums column maxima.
Approach
- Count cells with height > 0.
- Add the maximum of each row.
- Add the maximum of each column.
Why it works
Looking down, each occupied cell contributes one unit; looking from the side, each row's tallest tower defines that row's silhouette; likewise columns from the front.
Complexity
- Time —
O(n²) - Space —
O(1)
Pitfalls
- A cell with height 0 contributes nothing to the top view.
Reference solution
Python
def projectionArea(grid) -> int:
n = len(grid)
top = sum(1 for r in range(n) for c in range(n) if grid[r][c] > 0)
side = sum(max(row) for row in grid)
front = sum(max(grid[r][c] for r in range(n)) for c in range(n))
return top + front + sideJava
public static int projectionArea(int[][] grid) {
int n = grid.length;
int top = 0, front = 0, side = 0;
for (int r = 0; r < n; r++) {
int rowMax = 0, colMax = 0;
for (int c = 0; c < n; c++) {
if (grid[r][c] > 0) top++;
if (grid[r][c] > rowMax) rowMax = grid[r][c];
if (grid[c][r] > colMax) colMax = grid[c][r];
}
side += rowMax;
front += colMax;
}
return top + front + side;
}C++
int projectionArea(vector<vector<int>>& grid) {
int n = (int) grid.size();
int top = 0, front = 0, side = 0;
for (int r = 0; r < n; r++) {
int rowMax = 0, colMax = 0;
for (int c = 0; c < n; c++) {
if (grid[r][c] > 0) top++;
rowMax = max(rowMax, grid[r][c]);
colMax = max(colMax, grid[c][r]);
}
side += rowMax;
front += colMax;
}
return top + front + side;
}JavaScript
var projectionArea = function(grid) {
const n = grid.length;
let top = 0, front = 0, side = 0;
for (let r = 0; r < n; r++) {
let rowMax = 0, colMax = 0;
for (let c = 0; c < n; c++) {
if (grid[r][c] > 0) top++;
if (grid[r][c] > rowMax) rowMax = grid[r][c];
if (grid[c][r] > colMax) colMax = grid[c][r];
}
side += rowMax;
front += colMax;
}
return top + front + side;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Matrix and Grid Traversal