C#LeetCode刷题之#892-三维形体的表面积(Surface Area of 3D Shapes)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4136 访问。
在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体。
每个值 v = grid[i][j] 表示 v 个正方体叠放在单元格 (i, j) 上。
返回结果形体的总表面积。
输入:[[2]]
输出:10
示例 2:输入:[[1,2],[3,4]]
输出:34
示例 3:输入:[[1,0],[0,2]]
输出:16
示例 4:输入:[[1,1,1],[1,0,1],[1,1,1]]
输出:32
示例 5:输入:[[2,2,2],[2,1,2],[2,2,2]]
输出:46
提示:
- 1 <= N <= 50
- 0 <= grid[i][j] <= 50
On a N * N grid, we place some 1 * 1 * 1 cubes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
Return the total surface area of the resulting shapes.
Input: [[2]]
Output: 10
Example 2:Input: [[1,2],[3,4]]
Output: 34
Example 3:Input: [[1,0],[0,2]]
Output: 16
Example 4:Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 32
Example 5:Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 46
Note:
- 1 <= N <= 50
- 0 <= grid[i][j] <= 50
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4136 访问。
public class Program {
public static void Main(string[] args) {
var grid = new int[][] { new int[] { 2 } };
var res = SurfaceArea(grid);
Console.WriteLine(res);
Console.ReadKey();
}
public static int SurfaceArea(int[][] grid) {
var res = 0;
for(var i = 0; i < grid.Length; i++)
for(var j = 0; j < grid[0].Length; j++) {
if(grid[i][j] > 0)
//若有立方体,总的表面积为立方体的 4 个边加上下底
res += 4 * grid[i][j] + 2;
if(i < grid.Length - 1)
//若在 x 轴上,不是 x 轴上的最后一个
//那么减去它们重叠的部分 * 2
res -= Math.Min(grid[i][j], grid[i + 1][j]) * 2;
if(j < grid[0].Length - 1)
//若在 y 轴上,不是 y 轴上的最后一个
//那么减去它们重叠的部分 * 2
res -= Math.Min(grid[i][j], grid[i][j + 1]) * 2;
}
return res;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4136 访问。
10
分析:
显而易见,以上算法的时间复杂度为: 。
最新文章
- C++ std::multimap
- C#Winform连接Oracle数据库 , 及角色讲解
- P4行为模型BMV2依赖关系安装:thrift nanomsg nnpy安装
- Docker上运行dotnet core
- abstract修饰符
- 深入浅出CChart 每日一课——第十六课 实习之旅,百年老店之新锐WTL
- JavaScript作用域问题
- 团队作业4——第一次项目冲刺(Alpha版本)4.25
- Linux系列教程(二十一)——Linux的bash基本功能
- 关于java中的数组
- ubuntu1604使用源码方式安装ruby2.5.0
- 新版MATERIAL DESIGN 官方动效指南(一)
- postgresql从timestamp(6)复制到timestamp(0),时间会变
- 【PAT】B1037 在霍格沃茨找零钱(20 分)
- 【剑指offer】二叉搜索树与双向链表
- 牛客网NOIP赛前集训营-提高组(第一场)A 中位数
- Ubuntu下安装JDK图文教程详解 jdk-java6-30 .bin 的处理方法
- linux系统中安装JDK 查看安装的ava版本
- MyBatis之Collection
- css 字体、文本、padding的样式