# LeetCode: Brick Wall Solution

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/*2  bricks: [1, 2, 2, 1]3  edges: [1, 3, 5] (reject last one)4  min cross brick = number of wall - maximum crossed edges5*/6
7/**8 * @param {number[][]} wall9 * @return {number}10 */11var leastBricks = function (wall) {12  const edges = wall.flatMap(w => {13    w.pop()14    let sum = 015    for (let i = 0; i < w.length; i++) {16      w[i] += w[i - 1] || 017      sum += w[i]18    }19    return w20  })21
22  const occurrences = Array.from(23    edges24      .reduce((acc, el) => acc.set(el, (acc.get(el) || 0) + 1), new Map())25      .values()26  )27
28  return wall.length - Math.max.apply(null, [0, ...occurrences])29}```

## Comments

Loading comments...

leetcode

hash table

## Next Post

LeetCode: Count Binary Substrings

Apr 24, 2021

## Previous Post

LeetCode: Triangle

Apr 21, 2021

Memoized recursion

Search Posts