
😎풀이
- 현재 셀의 블록 높이를 구함
- 현재 블록의 윗면과 아랫면은 겹칠 수 없으므로 미리 더함
+= 2
- 양옆과 앞뒤를 확인하며, 인접한 블록이 있는 경우 해당 높이만큼 뺀 면적을 총 면적에 더함
- 총 면적을 반환함
function surfaceArea(grid: number[][]): number {
const n = grid.length
let area = 0
const direction = [[-1, 0], [0, 1], [1, 0], [0, -1]]
for(let y = 0; y < n; y++) {
for(let x = 0; x < n; x++) {
const curHeight = grid[y][x]
if(!curHeight) continue
area += 2
for(const [dy, dx] of direction) {
const ny = y + dy
const nx = x + dx
if(nx >= n || nx < 0 || ny >= n || ny < 0) area += curHeight
else {
const adjHeight = grid[ny][nx]
area += Math.max(curHeight - adjHeight, 0)
}
}
}
}
return area
};