问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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

分析:

显而易见,以上算法的时间复杂度为:  。

最新文章

  1. C++ std::multimap
  2. C#Winform连接Oracle数据库 , 及角色讲解
  3. P4行为模型BMV2依赖关系安装:thrift nanomsg nnpy安装
  4. Docker上运行dotnet core
  5. abstract修饰符
  6. 深入浅出CChart 每日一课——第十六课 实习之旅,百年老店之新锐WTL
  7. JavaScript作用域问题
  8. 团队作业4——第一次项目冲刺(Alpha版本)4.25
  9. Linux系列教程(二十一)——Linux的bash基本功能
  10. 关于java中的数组
  11. ubuntu1604使用源码方式安装ruby2.5.0
  12. 新版MATERIAL DESIGN 官方动效指南(一)
  13. postgresql从timestamp(6)复制到timestamp(0),时间会变
  14. 【PAT】B1037 在霍格沃茨找零钱(20 分)
  15. 【剑指offer】二叉搜索树与双向链表
  16. 牛客网NOIP赛前集训营-提高组(第一场)A 中位数
  17. Ubuntu下安装JDK图文教程详解 jdk-java6-30 .bin 的处理方法
  18. linux系统中安装JDK 查看安装的ava版本
  19. MyBatis之Collection
  20. css 字体、文本、padding的样式

热门文章

  1. 使用MapReduce运行WordCount案例
  2. threadLocal源码土话解说
  3. 设计模式:Iterator模式
  4. 面试题千变万化,为什么总是会问MySQL?
  5. 题解 洛谷 P4098 【[HEOI2013]ALO 】
  6. 【Nginx】并发量太高,Nginx扛不住?这次我错怪Nginx了!!
  7. MYSQL的事物四大特性
  8. 部分浏览器 set-cookie 不成功踩坑记录
  9. PHP is_null() 函数
  10. PHP imageantialias - 是否使用抗锯齿(antialias)功能