# Leetcode – Projection Area of 3D Shapes Solution in Python

Geometry often finds its way into computer science problems, presenting unique challenges that blend mathematical understanding with algorithmic techniques. The “Projection Area of 3D Shapes” problem on LeetCode is one such interesting puzzle that deals with 3D geometrical figures and their projections. In this detailed article, we will walk through the problem statement, insights, potential solutions, and a Python implementation.

## Problem Statement

On a 2D plane, we place N squares of different heights. They are aligned in a straight line, one next to the other. The i-th square has side length grid[i].length and a height of grid[i][j].

We want to view these squares from three different viewpoints: from the top, from the front, and from the side. These viewpoints will result in three different types of shadow projections on the ground.

The task is to compute the total area of all three projections.

### Constraints:

• 1 <= grid.length = grid.length <= 50
• 0 <= grid[i][j] <= 50

### Examples:

1. Input: [] Output: 5
2. Input: [[1,2],[3,4]] Output: 17
3. Input: [[1,0],[0,2]] Output: 8

## Analysis

Let’s break down the projections:

1. Top View: This will be the sum of all squares that have a height greater than zero. Essentially, every cell in the grid that’s non-zero will contribute one unit to the area.
2. Side View: This will be the sum of the maximum heights of each row. Imagine looking at the shapes from the side; you’d only see the tallest shape in each row.
3. Front View: Similarly, this will be the sum of the maximum heights of each column. From the front view, you’d see the tallest shape in each column.

By summing the areas from these three viewpoints, we get the total projected area.

## Solution Strategy

1. Compute the top view by counting the number of cells in the grid that have a value greater than zero.
2. Compute the side view by iterating over each row and adding the maximum value in that row.
3. Compute the front view by iterating over each column and adding the maximum value in that column.

Sum the results from the three steps to get the total projected area.

### Python Code:

class Solution:
def projectionArea(self, grid: List[List[int]]) -> int:
# Initialize the result to zero
result = 0

# Compute the top view
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] > 0:
result += 1

# Compute the side view
for row in grid:
result += max(row)

# Compute the front view
for j in range(len(grid)):
column_max = 0
for i in range(len(grid)):
column_max = max(column_max, grid[i][j])
result += column_max

return result

## Complexity Analysis

1. Time Complexity: The algorithm iterates over each element of the grid twice (once for the top view and once for the side view) and once over each column, making the overall time complexity O(N^2), where N is the side length of the grid.
2. Space Complexity: We use only a constant amount of space regardless of the input size. Thus, the space complexity is O(1).

## Conclusion

The Projection Area of 3D Shapes problem is a fun blend of geometry and algorithmic thinking. It underscores the importance of understanding the problem domain (in this case, basic geometry) to derive a solution. By breaking down the problem into individual projections, the solution becomes more straightforward and intuitive.

Furthermore, this problem showcases that not all computational challenges are purely about complex algorithms or advanced data structures. Sometimes, a bit of creative thinking and domain understanding can simplify the path to an efficient solution.