Open Zheaoli opened 2 years ago
微信id:Dapeus
/**
* @param {number[][]} grid
* @return {number}
* @link https://leetcode-cn.com/problems/projection-area-of-3d-shapes/
* @author 远浅
*/
var projectionArea = function(grid) {
const c = [];
let maxRow = 0;
const top = grid.reduce((pre, cur) => {
cur.forEach((item, index) => {
if (Array.isArray(c[index])) {
c[index].push(item);
} else {
c[index] = [item];
}
});
// 计算侧视图 每一行最大值相加
maxRow += Math.max(...cur);
// 计算俯视图面积 不为0的数值个数
return pre + cur.filter((i) => i > 0).length;
}, 0);
// 计算正视图面积 每一列最大值相加
const maxColumn = c.reduce((pre, cur) => {
return pre + Math.max(...cur);
}, 0);
return maxRow + top + maxColumn;
};
package main
/*
* @lc app=leetcode.cn id=883 lang=golang
*
* [883] 三维形体投影面积
*/
// @lc code=start
func Max(x, y int) int {
if x < y {
return y
}
return x
}
func projectionArea(grid [][]int) int {
res := 0
n := len(grid)
maxYZ := make([]int, n, n)
for x := 0; x < n; x++ {
maxZ := 0
for y := 0; y < n; y++ {
if grid[x][y] == 0 {
continue
}
res += 1
maxYZ[y] = Max(maxYZ[y], grid[x][y])
maxZ = Max(maxZ, grid[x][y])
}
res += maxZ
}
for _, maxZ := range maxYZ {
res += maxZ
}
return res
}
// func main() {
// fmt.Println(projectionArea([][]int{
// {1, 2},
// {3, 4},
// }))
// }
// @lc code=end
微信id: 而我撑伞 来自 vscode 插件
Wechat: Seventeen_Q
2022-04-26