Skip to content

Instantly share code, notes, and snippets.

@zaguiini
Last active December 2, 2019 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaguiini/af0399c5795529872f7772cab2e24c4a to your computer and use it in GitHub Desktop.
Save zaguiini/af0399c5795529872f7772cab2e24c4a to your computer and use it in GitHub Desktop.
Brick wall Leetcode solution
const wall = [
[1, 2, 2, 1],
[3, 1, 2],
[1, 2, 2],
[2, 4],
[3, 1, 2],
[1, 3, 1, 1]
]
const leastBricks = (wall) => {
const times = {}
for(let row of wall) {
let sum = 0
for(let j = 0; j < row.length - 1; j++) {
sum += row[j]
if(times[sum]) {
times[sum] += 1
} else {
times[sum] = 1
}
}
}
let maxBypassedBricks = 0
for(let sum of Object.values(times)) {
maxBypassedBricks = Math.max(maxBypassedBricks, sum)
}
return wall.length - maxBypassedBricks
}
console.log(leastBricks(wall))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment