883. Projection Area of 3D Shapes LeetCode Solution

In this guide, you will get 883. Projection Area of 3D Shapes LeetCode Solution with the best time and space complexity. The solution to Projection Area of D Shapes problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Projection Area of D Shapes solution in C++
  4. Projection Area of D Shapes solution in Java
  5. Projection Area of D Shapes solution in Python
  6. Additional Resources
883. Projection Area of 3D Shapes LeetCode Solution image

Problem Statement of Projection Area of D Shapes

You are given an n x n grid where we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of the cell (i, j).
We view the projection of these cubes onto the xy, yz, and zx planes.
A projection is like a shadow, that maps our 3-dimensional figure to a 2-dimensional plane. We are viewing the “shadow” when looking at the cubes from the top, the front, and the side.
Return the total area of all three projections.

Example 1:

Input: grid = [[1,2],[3,4]]
Output: 17
Explanation: Here are the three projections (“shadows”) of the shape made with each axis-aligned plane.

Example 2:

Input: grid = [[2]]
Output: 5

Example 3:

Input: grid = [[1,0],[0,2]]
Output: 8

Constraints:

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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

883. Projection Area of 3D Shapes LeetCode Solution in C++

class Solution {
 public:
  int projectionArea(vector<vector<int>>& grid) {
    int ans = 0;

    for (int i = 0; i < grid.size(); ++i) {
      int maxOfRow = 0;
      int maxOfCol = 0;
      for (int j = 0; j < grid.size(); ++j) {
        maxOfRow = max(maxOfRow, grid[i][j]);
        maxOfCol = max(maxOfCol, grid[j][i]);
        if (grid[i][j])
          ++ans;
      }
      ans += maxOfRow + maxOfCol;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

883. Projection Area of 3D Shapes LeetCode Solution in Java

class Solution {
  public int projectionArea(int[][] grid) {
    int ans = 0;

    for (int i = 0; i < grid.length; ++i) {
      int maxOfRow = 0;
      int maxOfCol = 0;
      for (int j = 0; j < grid.length; ++j) {
        maxOfRow = Math.max(maxOfRow, grid[i][j]);
        maxOfCol = Math.max(maxOfCol, grid[j][i]);
        if (grid[i][j] > 0)
          ++ans;
      }
      ans += maxOfRow + maxOfCol;
    }

    return ans;
  }
}
// code provided by PROGIEZ

883. Projection Area of 3D Shapes LeetCode Solution in Python

class Solution:
  def projectionArea(self, grid: list[list[int]]) -> int:
    return sum(
        a > 0 for row in grid for a in row) + sum(
        max(row) for row in grid) + sum(
        max(col) for col in zip(*grid))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.