Skip to content
LC-0892 Easy LeetCode

892. Surface Area of 3D Shapes

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 68% Topics: Array, Math, Geometry, Matrix
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n^2)
# Space: O(1)

class Solution(object):
    def surfaceArea(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        result = 0
        for i in xrange(len(grid)):
            for j in xrange(len(grid)):
                if grid[i][j]:
                    result += 2 + grid[i][j]*4
                if i:
                    result -= min(grid[i][j], grid[i-1][j])*2
                if j:
                    result -= min(grid[i][j], grid[i][j-1])*2
        return result

Solution from kamyu104/LeetCode-Solutions · MIT