↓Skip to main content
  1. LeetCode/

LeetCode 892: Surface Area of 3D Shapes

·2 mins· ·
LeetCode Easy Array Math Geometry Matrix
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-24

  • Submissions: Wrong Answer ×3 → Accepted

Approach
#

A lone stack of v > 0 cubes has 1 face on top, 1 below and 4v around. Two adjacent stacks hide min(a, b) faces on each side, so subtract 2 × min(a, b) (Matrix Template 3). Comparing each cell only with its upper and left neighbors avoids double subtraction.

My Attempts
#

  • Attempt 1 doubled the three projection areas, missing recessed faces: [[1, 3, 2], [3, 1, 0], [2, 1, 2]] returned 48 instead of 54.
  • Then I summed “how much taller than the previous” along rows and columns, but the column direction added the previous height std rather than the difference: [[3]] returned 8 instead of 14. Adding row[i] - std passed.

Solution
#

class Solution:
    def surfaceArea(self, grid: List[List[int]]) -> int:
        n = len(grid)
        area = 0
        for i in range(n):
            for j in range(n):
                v = grid[i][j]
                if v:
                    area += 4 * v + 2
                if i:
                    area -= 2 * min(v, grid[i - 1][j])
                if j:
                    area -= 2 * min(v, grid[i][j - 1])
        return area

Complexity
#

Time O(n²), space O(1).

Mistakes to Avoid
#

  • The surface area is not twice the projection area; recessed faces count too.
  • Subtract each adjacent pair once by looking only up and left.

Interview Explanation
#

Each stack of v cubes has 4v + 2 faces, and adjacent stacks hide the shorter height on both sides, so I subtract twice that, comparing each cell only with its upper and left neighbors. Time O(n²).

Related

LeetCode 883: Projection Area of 3D Shapes
·1 min
LeetCode Easy Array Math Geometry Matrix
LeetCode 883 note: the top view counts nonzero cells, and the front and side views sum each row’s and each column’s maximum
LeetCode 812: Largest Triangle Area
·2 mins
LeetCode Easy Array Math Geometry Polygons
LeetCode 812 note: with at most 50 points, enumerate every triple and take the largest shoelace area
LeetCode 867: Transpose Matrix
·1 min
LeetCode Easy Array Matrix Simulation
LeetCode 867 note: the transpose is n × m with res[j][i] = matrix[i][j]; in Python, zip(*matrix)
LeetCode 832: Flipping an Image
·2 mins
LeetCode Easy Array Two-Pointers Bit-Manipulation Matrix
LeetCode 832 note: reverse each row and invert its bits, which two pointers can do at once
LeetCode 836: Rectangle Overlap
·2 mins
LeetCode Easy Math Geometry
LeetCode 836 note: two rectangles overlap exactly when their x intervals and y intervals both overlap, i.e. min(right) > max(left)
LeetCode 766: Toeplitz Matrix
·2 mins
LeetCode Easy Array Matrix
LeetCode 766 note: every diagonal is constant exactly when each cell equals its upper-left neighbor