Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,2],[3,4]]
[[2]]
[[1,0],[0,2]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #883: Projection Area of 3D Shapes
class Solution {
public int projectionArea(int[][] grid) {
int xy = 0, yz = 0, zx = 0;
for (int i = 0, n = grid.length; i < n; ++i) {
int maxYz = 0;
int maxZx = 0;
for (int j = 0; j < n; ++j) {
if (grid[i][j] > 0) {
++xy;
}
maxYz = Math.max(maxYz, grid[i][j]);
maxZx = Math.max(maxZx, grid[j][i]);
}
yz += maxYz;
zx += maxZx;
}
return xy + yz + zx;
}
}
// Accepted solution for LeetCode #883: Projection Area of 3D Shapes
func projectionArea(grid [][]int) int {
xy, yz, zx := 0, 0, 0
for i, row := range grid {
maxYz, maxZx := 0, 0
for j, v := range row {
if v > 0 {
xy++
}
maxYz = max(maxYz, v)
maxZx = max(maxZx, grid[j][i])
}
yz += maxYz
zx += maxZx
}
return xy + yz + zx
}
# Accepted solution for LeetCode #883: Projection Area of 3D Shapes
class Solution:
def projectionArea(self, grid: List[List[int]]) -> int:
xy = sum(v > 0 for row in grid for v in row)
yz = sum(max(row) for row in grid)
zx = sum(max(col) for col in zip(*grid))
return xy + yz + zx
// Accepted solution for LeetCode #883: Projection Area of 3D Shapes
impl Solution {
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
let xy: i32 = grid
.iter()
.map(|row| row.iter().filter(|&&v| v > 0).count() as i32)
.sum();
let yz: i32 = grid.iter().map(|row| *row.iter().max().unwrap_or(&0)).sum();
let zx: i32 = (0..grid[0].len())
.map(|i| grid.iter().map(|row| row[i]).max().unwrap_or(0))
.sum();
xy + yz + zx
}
}
// Accepted solution for LeetCode #883: Projection Area of 3D Shapes
function projectionArea(grid: number[][]): number {
const xy: number = grid.flat().filter(v => v > 0).length;
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
const zx: number = grid[0]
.map((_, i) => Math.max(...grid.map(row => row[i])))
.reduce((acc, val) => acc + val, 0);
return xy + yz + zx;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.