Problem statement

https://leetcode.com/problems/projection-area-of-3d-shapes/

Solution

The idea is for each row and column find maximum value and sum all these values: it will be area of front and side shapes, also we calculate area of top shapes as number of non-zero elements.

Complexity

Time complexity is O(mn), space complexity is O(1).

Code

class Solution:
    def projectionArea(self, grid):
        top   = sum(v != 0 for r in grid for v in r)
        front = sum(max(r) for r in grid)
        side  = sum(max(r) for r in zip(*grid))
        return top + front + side