Leetcode883.Projection Area of 3D Shapes三维形体投影面积
在 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三维形体投影面积的更多相关文章
- [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 ...
- Leetcode892.Surface Area of 3D Shapes三维形体的表面积
在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体. 每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上. 返回结果形体的总表面积. 示例 1: ...
- 【Leetcode_easy】883. Projection Area of 3D Shapes
problem 883. Projection Area of 3D Shapes 参考 1. Leetcode_easy_883. Projection Area of 3D Shapes; 完
- 883. Projection Area of 3D Shapes
问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求三视图面积. Input: [[1,2],[3,4]] Output: 17 Explanation ...
- [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 ...
- 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. ...
- [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 ...
- 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 【leetcode】883. Projection Area of 3D Shapes
题目如下: 解题思路:分别求出所有立方体的个数,各行的最大值之和,各列的最大值之和.三者相加即为答案. 代码如下: class Solution(object): def projectionArea ...
随机推荐
- dd- Linux必学的60个命令
1.作用 dd命令用来复制文件,并根据参数将数据转换和格式化. 2.格式 dd [options] 3.[opitions]主要参数 bs=字节:强迫 ibs=<字节>及obs=<字 ...
- 使用springboot上传文件至nginx代理服务器
nginx配置图片服务器 server { listen 8001; server_name image.xxx.com; proxy_set_header X-Forwarded-Host $hos ...
- 使用ResponseEntity进行返回json数据
在最近的项目中,与上位机进行数据传输时,上位机需要服务器的响应得知服务器是否正常运行,数据是否正常发送 在最近的调试中我使用ResponseEntity<Map<String,Object ...
- 动态库加载时GetLasterror();值总是126的原因
1.dll路径不正确,导致找不到dll文件. 2.有可能是你要载入的DLL在内部还需要载入其它的dll,而它不存在,同样会返回126错误代码.比如一个你给系统添加了一个PCI设备,像AD采集卡之类的, ...
- 02.Hibernate配置文件之映射配置文件
映射文件,即xxx.hbm.xml的配置文件 <class>标签:用来将类与数据库表建立映射关系 属性: name:类中的全路径 table:表名(如果类与表名一致,那么table属性可以 ...
- 模板:exlucas
求$C_n^m mod p$,其中p不是质数且不保证p能分解为几个不同质数的乘积(也就是不能用crt合并) #include<iostream> #include<cstdio> ...
- Redis多API开发
目录 Redis API支持 redis-py安装方式 Python 连接redis 直接连接 使用连接池连接 Windows 连接redis数据库 一.下载Redis Desktop Manager ...
- java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintV ...
- js 中直接调用和new的区别
var test = new Test(); // 这里的 test 是什么? 是一个 Test 对象吗?错!这里 test 是一个函数——Test 中返回的 function() { return ...
- Octave 软件的安装
每次安装软件都感觉是一种心痛的历程.下载安装,然后就跳出一堆的错误,之后就各种百度求救,然后就搞了大半天,有时候还搞不定. 最后,搞定的时候发现,原来这么简单,结果时间就这样浪费了,所以还是把这个过程 ...