9 Lines JavaScript


  • 0
    F
    function leastBricks(wall) {
        const map = new Map()
        wall.forEach((row) => row.reduce((x, w) => {
            const y = x + w
            if (map.has(y)) map.set(y, map.get(y) + 1)
            else map.set(y, 1)
            return y
        }, 0))
        const vals = [...map.values()].sort((a, b) => b - a)
        return vals.length >= 2 ? wall.length - vals[1] : wall.length
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.