KatInterview

928. Surface 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

Method Explanation

The problem asks for the total surface area of a 3D shape constructed from 1x1x1 cubes on an n x n grid. The key to solving this is to realize that the total surface area is the sum of all exposed faces of the cubes.

Solution

Surface Area of 3D Shapes in Python

class Solution:
    def surfaceArea(self, grid: List[List[int]]) -> int:
        n = len(grid)
        total_area = 0

        # Iterate through each cell of the grid
        for i in range(n):
            for j in range(n):
                v = grid[i][j]
                # If there is a tower of cubes
                if v > 0:
                    # Add area for top and bottom faces
                    total_area += 2

                    # For each of the 4 neighbors, calculate the exposed side area.
                    # The exposed area is max(0, current_height - neighbor_height).
                    # If a neighbor is out of bounds, its height is treated as 0.

                    # Neighbor above
                    up_v = grid[i - 1][j] if i > 0 else 0
                    total_area += max(0, v - up_v)

                    # Neighbor below
                    down_v = grid[i + 1][j] if i < n - 1 else 0
                    total_area += max(0, v - down_v)

                    # Neighbor to the left
                    left_v = grid[i][j - 1] if j > 0 else 0
                    total_area += max(0, v - left_v)

                    # Neighbor to the right
                    right_v = grid[i][j + 1] if j < n - 1 else 0
                    total_area += max(0, v - right_v)
        
        return total_area

Surface Area of 3D Shapes in Java

class Solution {
    public int surfaceArea(int[][] grid) {
        int n = grid.length;
        int totalArea = 0;

        // Iterate through each cell of the grid
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int v = grid[i][j];
                // If there is a tower of cubes
                if (v > 0) {
                    // Add area for top and bottom faces
                    totalArea += 2;

                    // For each of the 4 neighbors, calculate the exposed side area.
                    // The exposed area is max(0, current_height - neighbor_height).
                    // If a neighbor is out of bounds, its height is treated as 0.

                    // Neighbor above
                    int upV = (i > 0) ? grid[i - 1][j] : 0;
                    totalArea += Math.max(0, v - upV);

                    // Neighbor below
                    int downV = (i < n - 1) ? grid[i + 1][j] : 0;
                    totalArea += Math.max(0, v - downV);

                    // Neighbor to the left
                    int leftV = (j > 0) ? grid[i][j - 1] : 0;
                    totalArea += Math.max(0, v - leftV);

                    // Neighbor to the right
                    int rightV = (j < n - 1) ? grid[i][j + 1] : 0;
                    totalArea += Math.max(0, v - rightV);
                }
            }
        }
        return totalArea;
    }
}

Surface Area of 3D Shapes in C++

class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int n = grid.size();
        int total_area = 0;

        // Iterate through each cell of the grid
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int v = grid[i][j];
                // If there is a tower of cubes
                if (v > 0) {
                    // Add area for top and bottom faces
                    total_area += 2;

                    // For each of the 4 neighbors, calculate the exposed side area.
                    // The exposed area is max(0, current_height - neighbor_height).
                    // If a neighbor is out of bounds, its height is treated as 0.

                    // Neighbor above
                    int up_v = (i > 0) ? grid[i - 1][j] : 0;
                    total_area += std::max(0, v - up_v);

                    // Neighbor below
                    int down_v = (i < n - 1) ? grid[i + 1][j] : 0;
                    total_area += std::max(0, v - down_v);

                    // Neighbor to the left
                    int left_v = (j > 0) ? grid[i][j - 1] : 0;
                    total_area += std::max(0, v - left_v);

                    // Neighbor to the right
                    int right_v = (j < n - 1) ? grid[i][j + 1] : 0;
                    total_area += std::max(0, v - right_v);
                }
            }
        }
        return total_area;
    }
};

Surface Area of 3D Shapes in TypeScript

function surfaceArea(grid: number[][]): number {
    const n = grid.length;
    let totalArea = 0;

    // Iterate through each cell of the grid
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            const v = grid[i][j];
            // If there is a tower of cubes
            if (v > 0) {
                // Add area for top and bottom faces
                totalArea += 2;

                // For each of the 4 neighbors, calculate the exposed side area.
                // The exposed area is max(0, current_height - neighbor_height).
                // If a neighbor is out of bounds, its height is treated as 0.

                // Neighbor above
                const upV = i > 0 ? grid[i - 1][j] : 0;
                totalArea += Math.max(0, v - upV);

                // Neighbor below
                const downV = i < n - 1 ? grid[i + 1][j] : 0;
                totalArea += Math.max(0, v - downV);

                // Neighbor to the left
                const leftV = j > 0 ? grid[i][j - 1] : 0;
                totalArea += Math.max(0, v - leftV);

                // Neighbor to the right
                const rightV = j < n - 1 ? grid[i][j + 1] : 0;
                totalArea += Math.max(0, v - rightV);
            }
        }
    }
    return totalArea;
};

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.