Last active
December 2, 2019 03:22
-
-
Save zaguiini/af0399c5795529872f7772cab2e24c4a to your computer and use it in GitHub Desktop.
Brick wall Leetcode solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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