You are given an n x ngrid 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.

Input: grid =[[1,2],[3,4]] Output:17 Explanation: Here are the three projections("shadows") of the shape made with each axis-aligned plane.
classSolution {
publicintprojectionArea(int[][] grid) {
int n = grid.length, ans = 0;
for (int i = 0; i < n; ++i) {
int rowMax = 0, colMax = 0;
for (int j = 0; j < n; ++j) {
if (grid[i][j]> 0) ans++;
rowMax = Math.max(rowMax, grid[i][j]);
colMax = Math.max(colMax, grid[j][i]);
}
ans += rowMax + colMax;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funprojectionArea(grid: Array<IntArray>): Int {
val n = grid.size
var ans = 0for (i in0 until n) {
var rowMax = 0var colMax = 0for (j in0 until n) {
if (grid[i][j] > 0) ans++ rowMax = maxOf(rowMax, grid[i][j])
colMax = maxOf(colMax, grid[j][i])
}
ans += rowMax + colMax
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from typing import List
classSolution:
defprojectionArea(self, grid: List[List[int]]) -> int:
n = len(grid)
ans =0for i in range(n):
row_max = col_max =0for j in range(n):
if grid[i][j] >0:
ans +=1 row_max = max(row_max, grid[i][j])
col_max = max(col_max, grid[j][i])
ans += row_max + col_max
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnprojection_area(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
letmut ans =0;
for i in0..n {
letmut row_max =0;
letmut col_max =0;
for j in0..n {
if grid[i][j] >0 { ans +=1; }
row_max = row_max.max(grid[i][j]);
col_max = col_max.max(grid[j][i]);
}
ans += row_max + col_max;
}
ans
}
}