Leetcode Note: Go - Surface Area of 3d Shapes

Surface Area of 3D Shapes - LeetCode
https://leetcode.com/problems/surface-area-of-3d-shapes/

  • Go 言語で取り組んだメモ

所感

  • 3d shapes の面積を計算して int で return する

回答

Golang solution 100%, 100%, with explanation and images - LeetCode Discuss
https://leetcode.com/problems/surface-area-of-3d-shapes/discuss/1149012/Golang-solution-100-100-with-explanation-and-images

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

func surfaceArea(grid [][]int) int {
    // 2 + shape * 4 == area of each shape
    res := 0

    for i := 0; i < len(grid); i++ {
        for j := 0; j < len(grid[0]); j++ {
            if grid[i][j] != 0 {
                res += 2 + grid[i][j]*4
            }
            if i-1 >= 0 {
                res -= 2 * min(grid[i-1][j], grid[i][j])
            }
            if j-1 >= 0 {
                res -= 2 * min(grid[i][j-1], grid[i][j])
            }
        }
    }

    return res
}