883.三维形体截面积(javascript)883.ProjectionAreaof3DShapes

原创
小哥 3年前 (2022-11-10) 阅读数 8 #大杂烩

在 n x n 的网格 grid 在里面,我们放了一些 x,y,z 三轴对齐 1 x 1 x 1 立方体。

每个值 v = grid[i][j] 表示 v 立方体堆叠在一个单元中。 (i, j) 上。

现在,我们看看这些立方体。 xy 、yz 和 zx 平面上的投影。

投影 像影子一样,威尔 三维 窗体映射到 二维 在飞机上。当从顶部、正面和侧面看立方体时,我们会看到“阴影”。

返回 所有三个投影的总面积 。

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

Return the total area of all three projections.

示例 1:

输入:[[1,2],[3,4]]
输出:17
说明:这是形状在三个轴对齐平面上的三个投影。(“阴影部分”)。

示例 2:

输入:grid = [[2]]
输出:5

示例 3:

输入:[[1,0],[0,2]]
输出:8

[[1,2],[3,4]]
根据问题的含义,xy对于每个项目0数字的总和(有多少个方块就有多少个项目)
xz获取所有列的最大值之和
yz获取所有行的最大值之和。

var projectionArea = function (grid) {
    let n = grid.length
    let xy = 0
    let xz = 0
    let yz = 0
    for (let i = 0; i < n; i++) {
        let xzHeight = 0
        let yzGetMax = 0
        let m = grid[i].length
        for (let j = 0; j < m; j++) {
            xy += grid[i][j] > 0 ? 1 : 0;
            xzHeight = Math.max(xzHeight, grid[j][i]);
            yzGetMax = Math.max(yzGetMax, grid[i][j]);
        }
        xz += xzHeight
        yz += yzGetMax
    }
    return xy + yz + xz
};

leetcode: https://leetcode.cn/problems/projection-area-of-3d-shapes/

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除