LeetCode: Brick Wall Solution
1/*2 bricks: [1, 2, 2, 1]3 edges: [1, 3, 5] (reject last one)4 min cross brick = number of wall - maximum crossed edges5*/67/**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 })2122 const occurrences = Array.from(23 edges24 .reduce((acc, el) => acc.set(el, (acc.get(el) || 0) + 1), new Map())25 .values()26 )2728 return wall.length - Math.max.apply(null, [0, ...occurrences])29}
Comments
Loading comments...
Tags
leetcode
hash table
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
LeetCode: Triangle
Memoized recursion