为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode892. 三维形体的表面积 | Surface Area of 3D Shapes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10605299.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

Example 1:

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

在 N * N 的网格上,我们放置一些 1 * 1 * 1  的立方体。

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

返回结果形体的总表面积。 

示例 1:

输入:[[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

Runtime: 52 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func surfaceArea(_ grid: [[Int]]) -> Int {
 3         var res:Int = 0
 4         var n:Int = grid.count
 5         for i in 0..<n
 6         {
 7             for j in 0..<n
 8             {
 9                 if grid[i][j] > 0
10                 {
11                     res += grid[i][j] * 4 + 2
12                 }
13                 if i > 0
14                 {
15                     res -= min(grid[i][j], grid[i - 1][j]) * 2
16                 }
17                 if j > 0
18                 {
19                     res -= min(grid[i][j], grid[i][j - 1]) * 2
20                 }
21             }
22         }
23         return res
24     }
25 }

60ms

 1 class Solution {
 2     func surfaceArea(_ grid: [[Int]]) -> Int {
 3         let n = grid.count
 4         var result = 0
 5         
 6         for i in 0..<n {
 7             for j in 0..<n {
 8                 let g = grid[i][j]
 9                 if g == 0 {
10                     continue
11                 }        
12                 result += 2
13                 
14                 if i > 0 {
15                     result += max(0, g-grid[i-1][j])
16                 } else {
17                     result += g
18                 }
19                 
20                 if j > 0 {
21                     result += max(0, g-grid[i][j-1])
22                 } else {
23                     result += g
24                 }
25                 
26                 if i+1 < n {
27                     result += max(0, g-grid[i+1][j])
28                 } else {
29                     result += g
30                 }
31                 
32                 if j+1 < n {
33                     result += max(0, g-grid[i][j+1])
34                 } else {
35                     result += g
36                 }
37             }
38         }
39         return result
40     }
41 }

88ms

 1 class Solution {
 2     func surfaceArea(_ grid: [[Int]]) -> Int {
 3         
 4         var sum=0,a=0,b=0,c=0,d=0
 5         for (i,item) in grid.enumerated() {
 6             for (j,subitem) in item.enumerated() {
 7                 if subitem > 0 {
 8                     sum += 2
 9                     a = j < grid[i].count-1 ? grid[i][j+1] : 0
10                     b = j > 0 ? grid[i][j-1] : 0
11                     c = i > 0 ? grid[i-1][j] : 0
12                     d = i < grid.count-1 ? grid[i+1][j] : 0
13                     sum += (max(subitem-a, 0)+max(subitem-b, 0)+max(subitem-c, 0)+max(subitem-d, 0))
14                 }
15             }
16         }
17         return sum
18     }
19 }

100ms

 1 class Solution {
 2     func surfaceArea(_ grid: [[Int]]) -> Int {
 3        func getSingleFaceArea(_ arr: [Int]) -> Int {
 4             var result = 0
 5             //上一个 x
 6             var lastX = 0
 7             arr.forEach { (x) in
 8                 //单独一堆的面积
 9                 let temp = (x == 0 ? 0 : (6 * x) - ((x - 1) * 2))
10                 result += temp
11                 let minX = lastX <= x ? lastX : x
12                 //减去两堆拼起来以后,遮住的面积
13                 result -= 2 * minX
14                 lastX = x
15             }
16             return result
17         }
18         
19         var result = 0
20         var lastArr: [Int] = []
21         for i in (0..<grid.count) {
22             let current = grid[i]
23             result += getSingleFaceArea(current)
24             if i != 0 {
25                 for i in (0..<current.count) {
26                     //减去两排拼起来以后,遮住的面积
27                     let x = lastArr[i] <= current[i] ? lastArr[i] : current[i]
28                     result -= x * 2
29                 }
30             }
31             lastArr = current
32         }
33         return result
34     }
35 }

 

posted @ 2019-03-27 09:27  为敢技术  阅读(260)  评论(0编辑  收藏  举报