KatInterview

919. Projection Area of 3D Shapes

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the total area of the projections of a 3D shape onto three orthogonal planes: the xy (top-down view), yz (front view), and zx (side view) planes. The 3D shape is constructed from 1x1x1 cubes on an n x n grid, where grid[i][j] specifies the height of the tower of cubes at cell (i, j).

Solution

Projection Area of 3D Shapes in Python

class Solution:
    def projectionArea(self, grid: List[List[int]]) -> int:
        # xy projection (top-down view): count of cells with cubes.
        # A cell has a cube if its height grid[i][j] is greater than 0.
        xy_area = sum(v > 0 for row in grid for v in row)
        
        # zx projection (side view): sum of the maximum heights in each row.
        zx_area = sum(max(row) for row in grid)
        
        # yz projection (front view): sum of the maximum heights in each column.
        # zip(*grid) is a Pythonic way to transpose a 2D list, effectively iterating over columns.
        yz_area = sum(max(col) for col in zip(*grid))
        
        return xy_area + zx_area + yz_area

Projection Area of 3D Shapes in Java

class Solution {
    public int projectionArea(int[][] grid) {
        int n = grid.length;
        int xyArea = 0; // Area of projection on xy-plane (top-down view)
        int yzArea = 0; // Area of projection on yz-plane (front view)
        int zxArea = 0; // Area of projection on zx-plane (side view)

        for (int i = 0; i < n; i++) {
            int maxRowVal = 0; // Max height in row i
            int maxColVal = 0; // Max height in column i

            for (int j = 0; j < n; j++) {
                // For xy projection, if there's a cube (height > 0), it casts a 1x1 shadow.
                if (grid[i][j] > 0) {
                    xyArea++;
                }

                // For zx projection (side view), find the max height in the current row `i`.
                maxRowVal = Math.max(maxRowVal, grid[i][j]);

                // For yz projection (front view), find the max height in the current column `i`.
                // We access grid[j][i] to iterate through the i-th column's elements.
                maxColVal = Math.max(maxColVal, grid[j][i]);
            }

            // The area contributed by this row to the zx projection is its max height.
            zxArea += maxRowVal;
            // The area contributed by this column to the yz projection is its max height.
            yzArea += maxColVal;
        }

        return xyArea + yzArea + zxArea;
    }
}

Projection Area of 3D Shapes in C++

class Solution {
public:
    int projectionArea(vector<vector<int>>& grid) {
        int n = grid.size();
        int xy_area = 0; // Area of projection on xy-plane (top-down view)
        int yz_area = 0; // Area of projection on yz-plane (front view)
        int zx_area = 0; // Area of projection on zx-plane (side view)

        for (int i = 0; i < n; ++i) {
            int max_row_val = 0; // Max height in row i
            int max_col_val = 0; // Max height in column i

            for (int j = 0; j < n; ++j) {
                // For xy projection, if there's a cube (height > 0), it casts a 1x1 shadow.
                if (grid[i][j] > 0) {
                    xy_area++;
                }

                // For zx projection (side view), find the max height in the current row `i`.
                max_row_val = max(max_row_val, grid[i][j]);

                // For yz projection (front view), find the max height in the current column `i`.
                // We access grid[j][i] to iterate through the i-th column's elements.
                max_col_val = max(max_col_val, grid[j][i]);
            }

            // The area contributed by this row to the zx projection is its max height.
            zx_area += max_row_val;
            // The area contributed by this column to the yz projection is its max height.
            yz_area += max_col_val;
        }

        return xy_area + yz_area + zx_area;
    }
};

Projection Area of 3D Shapes in TypeScript

function projectionArea(grid: number[][]): number {
    const n = grid.length;
    let xyArea = 0; // Area of projection on xy-plane (top-down view)
    let yzArea = 0; // Area of projection on yz-plane (front view)
    let zxArea = 0; // Area of projection on zx-plane (side view)

    for (let i = 0; i < n; i++) {
        let maxRowVal = 0; // Max height in row i
        let maxColVal = 0; // Max height in column i

        for (let j = 0; j < n; j++) {
            // For xy projection, if there's a cube (height > 0), it casts a 1x1 shadow.
            if (grid[i][j] > 0) {
                xyArea++;
            }

            // For zx projection (side view), find the max height in the current row `i`.
            maxRowVal = Math.max(maxRowVal, grid[i][j]);

            // For yz projection (front view), find the max height in the current column `i`.
            // We access grid[j][i] to iterate through the i-th column's elements.
            maxColVal = Math.max(maxColVal, grid[j][i]);
        }

        // The area contributed by this row to the zx projection is its max height.
        zxArea += maxRowVal;
        // The area contributed by this column to the yz projection is its max height.
        yzArea += maxColVal;
    }

    return xyArea + yzArea + zxArea;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.