# Surface Area of 3D Shapes in Python

PythonServer Side ProgrammingProgramming

Suppose there is a N x N grid, we place some 1 x 1 x 1 cubes. in it. Now for each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell(i, j). We have to find the total surface area of the resulting shapes.

So, if the input is like [[1,2],[3,4]], then the output will be 34.

To solve this, we will follow these steps −

• Define a function adjacentArea(). This will take row
• area := 0
• for i in range 0 to size of row - 1, do
• if row[i] and row[i + 1] is non-zero, then
• area := area + 2 * minimum of row[i], row[i+1]
• return area
• From the main method do the following −
• z := 2* (sum of (sum of value i in row) for all row in grid)
• x_plus_y := sum of all elements in the grid * 4
• y_adjacent := sum of adjacentArea(row) for all column in the grid

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def surfaceArea(self, grid):
area = 0
for i in range(len(row) - 1):
if row[i] and row[i + 1]:
area += 2 * min(row[i], row[i+1])
return area
z = sum([sum(i > 0 for i in row) for row in grid]) * 2
x_plus_y = sum([sum(row) for row in grid]) * 4
print(ob.surfaceArea([[1,2],[3,4]]))
[[1,2],[3,4]]
34