Skip to content

883. Projection Area of 3D Shapes

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

js
/**
 * @param {number[][]} grid
 * @return {number}
 */
var projectionArea = function(grid) {
  const rows = grid.length
  const cols = grid[0].length
  let x = new Array(rows).fill(0)
  let y = new Array(cols).fill(0)
  let z = 0

  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      let c = grid[i][j]
      if (c > 0) {
        z += 1
      }
      if (c > x[i]) {
        x[i] = c
      }
      if (c > y[j]) {
        y[j] = c
      }
    }
  }

  let total = z
  x.forEach(i => total += i)
  y.forEach(i => total += i)

  return total
}
py
class Solution:

    def projectionArea(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        rows = len(grid)
        cols = len(grid[0])
        x = [0] * rows
        y = [0] * cols
        z = 0

        for i in range(rows):
            for j in range(cols):
                c = grid[i][j]
                if c > 0:
                    z += 1
                if c > x[i]:
                    x[i] = c
                if c > y[j]:
                    y[j] = c

        return sum(x) + sum(y) + z