Problem
NxN lattice, stacked with 1x1x1 cubes, grid[i][j] represents the number of cubes stacked on a coordinate grid, and the three-view area is calculated.
Input: [[1,2],[3,4]]
Output:17
Explanation: See
Ideas
For the top view, as long as a lattice has a value, the area value is added 1.
For a front view (facing the x-axis), for an X, the highest grid value in the y-axis direction, which indicates that the x follows the y-axis to see the area values seen in the past.
For a side view (facing the y-axis), for a Y, the highest grid value in the x-axis direction, indicating that Y is looking at the area values seen in the past along the y-axis.
Add up these area values.
Code
class Solution(object): def projectionArea(self, grid): """ :type grid: List[List[int]] :rtype: int """ s = 0 n = len(grid) for i in range(n): best_row = 0 best_col = 0 for j in range(n): if(grid[i][j] > 0): s += 1 best_row = max(best_row, grid[i][j]) best_col = max(best_col, grid[j][i]) s += best_row + best_col return s
Similar topics
892. Surface area of 3D Shapes
883. Projection area of 3D Shapes