题目链接:
力扣https://leetcode-cn.com/problems/projection-area-of-3d-shapes/
【分析】侧面面积是以行或者列枚举的最大值,上面的面积是不为0的元素的个数,一次枚举数组中全部元素的过程中就可以得到行和列的最大值。
class Solution {
public int projectionArea(int[][] grid) {
int n = grid.length, a, b, ans = 0;
for(var i = 0; i < n; i++){
a = 0; b = 0;
for(var j = 0; j < n; j++){
ans += grid[i][j] == 0? 0: 1;
a = Math.max(a, grid[i][j]);
b = Math.max(b, grid[j][i]);
}
ans += (a + b);
}
return ans;
}
}