• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode883.三维形体投影面积|ProjectionAreaof3DShapes

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.

Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).

Now we view the projection of these cubes onto the xy, yz, and zx planes.

A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane. 

Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side.

Return the total area of all three projections.

Example 1:

Input: [[2]]
Output: 5

Example 2:

Input: [[1,2],[3,4]]
Output: 17
Explanation: 
Here are the three projections ("shadows") of the shape made with each axis-aligned plane.

Example 3:

Input: [[1,0],[0,2]]
Output: 8

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21 

Note:

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

在 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

48ms
 1 class Solution {
 2     func projectionArea(_ grid: [[Int]]) -> Int {
 3         var xy = 0, xz = 0, yz = [Int]()
 4         for row in grid {
 5             var z = 0
 6             for cell in 0..<row.count {
 7                 if row[cell] > 0 {
 8                     xy += 1
 9                 }
10                 z = row[cell] > z ? row[cell] : z
11                 if cell < yz.count {
12                     yz[cell] = row[cell] > yz[cell] ? row[cell] : yz[cell]
13                 } else {
14                     yz.append(row[cell])
15                 }
16                 
17             }
18             xz += z
19         }
20         return xy + xz + yz.reduce(0, +)
21     }
22 }

Runtime: 52 ms
Memory Usage: 19.3 MB
 1 class Solution {
 2     func projectionArea(_ grid: [[Int]]) -> Int {
 3         var z:Int = 0
 4         var x:Int = 0
 5         var y:Int = 0
 6         for i in 0..<grid.count
 7         {
 8             var mx:Int = 0
 9             var my:Int = 0
10             for j in 0..<grid.count
11             {
12                 mx = max(mx, grid[j][i])
13                 my = max(my, grid[i][j])
14                 if grid[i][j] > 0
15                 {
16                     z += 1
17                 }
18             }
19             x += mx
20             y += my
21         }
22         return x + y + z
23     }
24 }

52ms

 1 class Solution {
 2     func projectionArea(_ grid: [[Int]]) -> Int {
 3         guard grid.count > 0, grid[0].count > 0 else {
 4             return 0
 5         }
 6         var sumx = 0
 7         var sumy = 0
 8         var x = 0
 9         var y = 0
10         var z = 0
11         for i in 0..<grid.count {
12             x = 0
13             for j in 0..<grid[0].count {
14                 if grid[i][j] > 0 {
15                     z += 1
16                     x = max(x,grid[i][j])
17                 }
18             }
19             sumx += x
20         }
21         for i in 0..<grid[0].count {
22             y = 0
23             for j in 0..<grid.count {
24                 if grid[j][i] > 0 {
25                     y = max(y,grid[j][i])
26                 }
27             }
28             sumy += y
29         }
30         return sumx+sumy+z
31     }
32 }

56ms

1 class Solution {
2     func projectionArea(_ grid: [[Int]]) -> Int {
3         let topShadow = grid.map { r in r.filter { $0 != 0 }.count }.reduce(0, +)
4         let xShadow = grid.reduce(0) { return $0 + ($1.max() ?? 0) }
5         let yShadow = Array(0 ..< grid[0].count).map { c in grid.map {$0[c] }.max() ?? 0 }.reduce(0, +)
6         return topShadow + xShadow + yShadow
7     }
8 }

60ms

 1 class Solution {
 2     func projectionArea(_ grid: [[Int]]) -> Int {        
 3         var topCounter = 0
 4         var leftCounter = 0
 5         var rightCounter = 0
 6         for i in 0..<grid.count {
 7             var maxLine = 0
 8             for j in 0..<grid[0].count {
 9                 if grid[i][j] != 0 {
10                     topCounter += 1
11                 }
12                 maxLine = max(maxLine, grid[i][j])
13             }
14             leftCounter += maxLine
15         }
16         
17         for j in 0..<grid[0].count {
18             var maxCol = 0
19             for i in 0..<grid.count {
20                 maxCol = max(maxCol, grid[i][j])
21             }
22             rightCounter += maxCol
23         }
24         return topCounter + rightCounter + leftCounter
25     }
26 }

64ms

 1 class Solution {
 2     func projectionArea(_ grid: [[Int]]) -> Int {
 3         var total = 0
 4         
 5         for i in 0..<grid.count {
 6             var maxValue = 0
 7             for j in 0..<grid[0].count {
 8                 if grid[i][j] != 0 {
 9                     total += 1
10                 }
11                 maxValue = max(maxValue, grid[i][j])
12             }
13             total += maxValue
14         }
15         
16         for j in 0..<grid[0].count {
17             var maxValue = 0
18             for i in 0..<grid.count {
19                 maxValue = max(maxValue, grid[i][j])
20             }
21             total += maxValue
22         }
23         
24         return total
25     }
26 }

100ms

 1 class Solution {
 2     func projectionArea(_ grid: [[Int]]) -> Int {
 3         //每个子数组的 count 相加 (要剔除0)
 4         var x = 0
 5         //每个 index 的最大值相加
 6         var y = 0
 7         //每个子数组的最大值相加
 8         var z = 0
 9         //用于统计每个 index 的最大值 key 为 index
10         var dictY: [Int: Int] = [:]
11         grid.forEach { (sun) in
12             if let max = sun.max() {
13                 z += max
14             }
15             sun.enumerated().forEach({ (index, value) in
16                 if value != 0 {
17                     x += 1
18                 }
19                 if let tempY = dictY[index] {
20                     if tempY < value {
21                         //如果存在并小于,更新
22                         dictY.updateValue(value, forKey: index)
23                     }
24                 }else {
25                     //不存在直接更新
26                     dictY.updateValue(value, forKey: index)
27                 }
28             })
29         }
30         dictY.values.forEach{y += $0}
31         return x + y + z
32     }
33 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[RxSwift]RxSwift:ReactiveXforSwift发布时间:2022-07-13
下一篇:
[Swift]LeetCode504.七进制数|Base7发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap