Created
February 26, 2019 06:54
-
-
Save imgen/cb76236986d4c24501af3f41209652f7 to your computer and use it in GitHub Desktop.
My friend CM's JavaScript version of calculating max rectangle area
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 f = a => { | |
let max = 0; | |
const getLocalMax = a => { | |
let localMax = 0, maxJ = a[0].length -1; | |
const reset = i => { | |
const size = (i+1)*(maxJ+1); | |
if(size > localMax) | |
localMax = size; | |
} | |
a.some( (row, i) => { | |
row.split('').some( (data, j) => { | |
if(j > maxJ) | |
return true; | |
if(!+data) { | |
maxJ = j - 1; | |
return true; | |
} | |
}); | |
reset(i); | |
}); | |
return localMax; | |
}; | |
a.forEach( (row, i) => { | |
row.split('').forEach( (_, j) => { | |
const currentArr = a.slice(i).map( r => r.slice(j) ); | |
let localMax = getLocalMax( currentArr ); | |
if(localMax > max) | |
max = localMax; | |
}) | |
}); | |
return max; | |
}; | |
var matrices = [ | |
[ | |
"1111", | |
"0110", | |
"1110", | |
"1111" | |
], | |
[ | |
"11011", | |
"01101", | |
"11110", | |
"11111", | |
"01111" | |
], | |
[ | |
"101101", | |
"111111", | |
"011111", | |
"111111", | |
"001111", | |
"011111" | |
] | |
]; | |
for (var i = 0; i < matrices.length; i++) { | |
var matrix = matrices[i]; | |
var maxArea = f(matrix); | |
console.log("The max area is " + maxArea); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment