在 N * N 的网格中,我们放置了一些与 x,y,z 三轴对齐的 1 * 1 * 1 立方体。

每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上。

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

投影就像影子,将三维形体映射到一个二维平面上。

在这里,从顶部、前面和侧面看立方体时,我们会看到“影子”。

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

示例 1:

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

示例 2:

输入:[[1,2],[3,4]] 输出:17 解释: 这里有该形体在三个轴对齐平面上的三个投影(“阴影部分”)。

示例 3:

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

示例 4:

输入:[[1,1,1],[1,0,1],[1,1,1]] 输出:14

示例 5:

输入:[[2,2,2],[2,1,2],[2,2,2]] 输出:21

提示:

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

投影在xy平面上,表示与z轴无关,

投影在yz平面上,表示与x轴无关

投影在xz平面上,表示与y轴无关

class Solution {
public:
int projectionArea(vector<vector<int> >& grid)
{
int xy = 0;
int r = grid.size();
int c = grid[0].size();
map<int, int> xz;
map<int, int> yz;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(grid[i][j] > 0)
{
xy++;
if(xz[i] != 0)
{
xz[i] = max(grid[i][j], xz[i]);
}
else
{
xz[i] = grid[i][j];
}
if(yz[j] != 0)
{
yz[j] = max(grid[i][j], yz[j]);
}
else
{
yz[j] = grid[i][j];
}
}
}
}
int sum = xy;
for(map<int, int> :: iterator itr = xz.begin(); itr != xz.end(); itr++)
{
sum += itr ->second;
}
for(map<int, int> :: iterator itr = yz.begin(); itr != yz.end(); itr++)
{
sum += itr ->second;
}
return sum;
}
};

Leetcode883.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. Each ...

  2. Leetcode892.Surface Area of 3D Shapes三维形体的表面积

    在 N * N 的网格上,我们放置一些 1 * 1 * 1  的立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 返回结果形体的总表面积. 示例 1: ...

  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. 883. Projection Area of 3D Shapes

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

  5. [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 ...

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

  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 解题报告(Python)

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

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

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

随机推荐

  1. 【error】vue-style-loader didn't discriminate between server and client

    出现这个bug的时候,设置为false

  2. HZOI20190906模拟38 金,斯诺,赤

    题面:https://www.cnblogs.com/Juve/articles/11479415.html T1:高精度gcd,其实不用写高精度取模,gcd还有一种求法 int gcd(int a, ...

  3. 软件-UlitraEdit:UIitraEdit

    ylbtech-软件-UlitraEdit:UIitraEdit UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足够强大),内 ...

  4. js笔试-接收get请求参数

    请编写一个JavaScript函数,它的用途是接收url中get请求的参数,并返回为对象, 如: var url = “https://i.cnblogs.com/EditPosts.aspx?opt ...

  5. 进一步封装poco下的mysql操作

    为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 主 ...

  6. 用windows命令解压chm文件

    Windows里有这样一个工具:hh.exe.hh.exe最重要的功能就是用来关联CHM文件,当你运行一个chm文件的时候,系统就是用这个工具来打开的. 其实它还有另外一个功能——解压CHM文件在CM ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. processlist

    ###################### 当前会话的线程id,也就是会话id select connection_id(); ########################### select ...

  9. JAVA面试常见问题之锁机制篇

    1.说说线程安全问题,什么是线程安全,如何保证线程安全 线程安全:就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不 ...

  10. BootStrap框架选择

    1. mentronic4.0 效果非常好,但是商业版收费 下面是一个.net的系统,基于mentronic4.0开发,感觉不错 http://www.cnblogs.com/guozili/p/34 ...