LeetCode Solutions

883. Projection Area of 3D Shapes

Time:

Space:

			

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;
  }
};
			

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;
  }
}
			

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))