★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-xrcqosws-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
On a N * N
grid, we place some 1 * 1 * 1
cubes that are axis-aligned with the x, y, and z axes.git
Each value v = grid[i][j]
represents a tower of v
cubes placed on top of grid cell (i, j)
.github
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.app
Return the total area of all three projections.ide
Example 1:spa
Input: [[2]]
Output: 5
Example 2:code
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:htm
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
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 }
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 }