At first, the problem looks very difficult as it involves multi-dimensional array and projections but the graph in the example really gives out the clue about how to solve this problem.
class Solution {
fun projectionArea(grid: Array<IntArray>): Int {
val size = grid.size
var totalArea = 0
(0 until size).forEach { x ->
var xMax = 0
var yMax = 0
(0 until size).forEach { y ->
xMax = Math.max(xMax, grid[x][y])
yMax = Math.max(yMax, grid[y][x])
if(grid[x][y] > 0) totalArea++
}
totalArea += xMax + yMax
}
return totalArea
}
}
Runtime: 192 ms, faster than 100.00% of Kotlin online submissions for Projection Area of 3D Shapes.
Memory Usage: 36.7 MB, less than 100.00% of Kotlin online submissions for Projection Area of 3D Shapes.