On a `N * N` grid, we place some `1 * 1 * 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 grid cell (i, j).

Now 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.

Here, 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.

Example 1:

Input: [[2]]
Output: 5

Example 2:

Input: [[1,2],[3,4]]
Output: 17
Explanation:
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.



Example 3:

Input: [[1,0],[0,2]]
Output: 8

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21

Note:

  • 1 <= grid.length = grid[0].length <= 50
  • 0 <= grid[i][j] <= 50

这道题给了我们一个二维数组 grid,用来表示一个 3D 物体形状,表示方法是 grid[i][j] 表示在 (i, j) 位置上的高度,就像垒积木一样,累出了一个三维物体。然后让我们计算三个方向的投影面积之和,所谓的三个方向分别是上方 Top,前方 Front,和侧方 Side。用过一些三维建模软件(例如 Maya, 3DMax)的同学,对这个应该不陌生。我们先来考虑正上方投影面积如何计算,由于题目中说了 grid 数组的宽和高相等,那么上方投影就是一个正方形,前提是每个 grid[i][j] 的值都大于0的话。因为若 grid 数组中有0存在,则表示正方形投影会缺少了一块。由于这个大的正方形投影是由 nxn 个小的正方形组成,那么实际上我们只要统计出小正方形的个数,那么大正方形投影的面积也就知道了(是不有点微积分的感觉)。所以我们在遍历的过程中,只要判断若 grid[i][j] 大于0,则结果 res 自增1即可。下面再来考虑另外两个方向的投影怎么计算,另两个方向的投影的可能是不规则图形,参见题目中给的那个图,如果仔细观察的话,其投影图像的每个阶段的高其实就是各行或各列中的最大值,这也不难理解,就像城市中耸立的高度不同的大楼,若要描出城市的轮廓,那么描出来的肯定都是每个位置上最高建筑物的轮廓。那么问题就变成了累加各行各列的最大值。我们实际上在一次遍历中就能完成,使用了一个小 trick,那就是在第二层 for 循环中,行最大值 rowMax 就是不断用 grid[i][j] 来更新,而列最大值 colMax 就是不断用 grid[j][i] 来更新,巧妙的交换i和j,实现了目标。然后分别把更新出来的行列最大值加到结果 res 中即可,参见代码如下:

class Solution {
public:
int projectionArea(vector<vector<int>>& grid) {
int n = grid[0].size(), res = 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) ++res;
rowMax = max(rowMax, grid[i][j]);
colMax = max(colMax, grid[j][i]);
}
res += rowMax + colMax;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/883

参考资料:

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

https://leetcode.com/problems/projection-area-of-3d-shapes/discuss/156726/C%2B%2BJavaPython-Straight-Forward

https://leetcode.com/problems/projection-area-of-3d-shapes/discuss/156771/11-line-1-pass-Java-code-and-explanation-of-the-problem-time-O(N-2)-space-O(1).

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 883. Projection Area of 3D Shapes 三维物体的投影面积的更多相关文章

  1. LeetCode 883 Projection Area of 3D Shapes 解题报告

    题目要求 On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. ...

  2. Leetcode883.Projection Area of 3D Shapes三维形体投影面积

    在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 现在,我们查 ...

  3. 【Leetcode_easy】883. Projection Area of 3D Shapes

    problem 883. Projection Area of 3D Shapes 参考 1. Leetcode_easy_883. Projection Area of 3D Shapes; 完

  4. [LeetCode] 892. Surface Area of 3D Shapes 三维物体的表面积

    On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...

  5. 883. Projection Area of 3D Shapes

    问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求三视图面积. Input: [[1,2],[3,4]] Output: 17 Explanation ...

  6. 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  7. [LeetCode&Python] Problem 883. Projection Area of 3D Shapes

    On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each ...

  8. 【leetcode】883. Projection Area of 3D Shapes

    题目如下: 解题思路:分别求出所有立方体的个数,各行的最大值之和,各列的最大值之和.三者相加即为答案. 代码如下: class Solution(object): def projectionArea ...

  9. [Swift]LeetCode883. 三维形体投影面积 | Projection Area of 3D Shapes

    On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each ...

随机推荐

  1. 【shell脚本】显示进度条

    使用动态时针版本显示进度条 [root@VM_0_10_centos shellScript]# cat progressBar.sh #!/bin/bash # 进度条,动态时针版本 # 定义显示进 ...

  2. 【UOJ#82】【UR #7】水题生成器(贪心)

    [UOJ#82][UR #7]水题生成器(贪心) 题面 UOJ 题解 把\(n!\)的所有约数搜出来,这个个数不会很多. 然后从大往小能选则选就好了. #include<iostream> ...

  3. identityServer3+ADFS实现域用户登录授权

    准备: ADFS安装配置 https://www.cnblogs.com/luoyedemeng/articles/9837685.html 添加一个Providers private void Co ...

  4. 4-consul HTTP API及实践

    其他参考:https://www.cnblogs.com/duanxz/p/9660766.html 原文:https://www.douban.com/note/629645759/ 注意:使用AP ...

  5. Java生鲜电商平台-会员积分系统的设计与架构

    Java生鲜电商平台-会员积分系统的设计与架构 说明:互联网平台积分体系主要用于激励和回馈用户在平台的消费行为和活动行为,一个良好的积分体系可以很好的提升用户的粘性及活跃度. 一.互联网平台积分体系设 ...

  6. MySQL5.7增量备份恢复全实战

    一. 简介 1. 增量备份 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加或者被修改的文件.这就意味着,第一次增量 备份的对象是进行全备后所产生的增加和修改的文件; ...

  7. IOS开发实战-Xcode创建HelloWorld项目

    一.创建工程打开Xcode开发工具,在Welcome界面选择”Create a new Xcode project”选项 在选择模板窗口,选择”Single View Application” 确定模 ...

  8. 关于VS2015 发布.net mvc 网站失败的问题

    问题:VS生成成功,发布失败,在“正在连接到***文件夹”处就不能继续了.. 项目开发告一段落,准备部署到服务器上进行最后测试,但是始终发布失败  生成成功,发布失败,没有任何提示信息 一开始以为是文 ...

  9. jquery实现一些小动画二

    jquery实现一些小动画二 jquery实现拖拽功能 <!DOCTYPE html> <html lang="en"> <head> < ...

  10. vue导出文件下载

    项目当中有用到文件的导出功能,以此来总结 request({ /*url: this.exportUrl,*/ url: `************`, method: "GET" ...