Projection Area of 3D Shapes — LeetCode 883 Python Solution

EasyGeometryArrayMathMatrix
Problem
#883
Reading time
2 min

The problem

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).

Example

Input
grid = [[1,2],[3,4]]
Output
17
Explanation
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.

Python solution

Python
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

Complexity

MeasureComplexity
TimeO(n^2), where n is the side length of the grid `grid`
SpaceO(1) auxiliary

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 883. Projection Area of 3D Shapes is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.

The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 883. Projection Area of 3D Shapes?
LeetCode 883. Projection Area of 3D Shapes is rated Easy on LeetCode.
What is the time complexity of LeetCode 883. Projection Area of 3D Shapes?
The Python solution on this page runs in O(n^2), where n is the side length of the grid `grid`.
What is the space complexity of LeetCode 883. Projection Area of 3D Shapes?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 883. Projection Area of 3D Shapes cover?
LeetCode 883. Projection Area of 3D Shapes is tagged Geometry, Array, Math and Matrix on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview