Skip to content

Instantly share code, notes, and snippets.

@FelixLuciano
Last active January 16, 2021 05:51
Show Gist options
  • Save FelixLuciano/ba07af8a3ccc2b030f7047411fbfbdd7 to your computer and use it in GitHub Desktop.
Save FelixLuciano/ba07af8a3ccc2b030f7047411fbfbdd7 to your computer and use it in GitHub Desktop.
Given a n x m binary matrix filled with 0s and 1s, find the largest rectangle containing only 1s and return its area.
function largestRect(matrix, match = "1") {
let maxArea = 0
for (let y1 of matrix.keys()) {
for (let x1 of matrix[y1].keys()) {
if (matrix[y1][x1] !== match) continue
for (var x2 = x1; matrix[y1][x2 + 1] === match; x2++) continue
for (var y2 = y1; matrix[y2 + 1]?.slice(x1, x2 + 1).every(a => a === match); y2++) continue
maxArea = Math.max(maxArea, (x2 - x1 + 1) * (y2 - y1 + 1))
}
}
return maxArea
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment