• + 0 comments

    javascript

    function surfaceArea(A) {
        let besides = [[1,0],[-1,0],[0,1],[0,-1]]
        let surface = 0
        for(let i=0; i<A.length; i++){
            for(let j=0; j<A[0].length; j++){
                surface += 2
                for(let [r,h] of besides){
                   surface += Math.max(A[i][j] - (A[i+r]?.[j+h] || 0), 0);
                }
            }
        }
        return surface
    }