Created
January 12, 2021 20:24
-
-
Save pricklywiggles/9cf738ccc28e2cf26e25018fdeb675bc to your computer and use it in GitHub Desktop.
Casidoo-178
This file contains 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
// Given an array of integers representing the number of consecutive 1s immediately stacked vertically at this index | |
// calculate the largest area by grabbing the largest of all rectangular areas calculated at each index. | |
let largestRowArea = row => { | |
let largestArea = 0; | |
row.forEach((value, i) => { | |
let area = value; | |
if (value != 0) { | |
for(let j=i-1; j>=0; j--) { | |
if (row[j] < value) break; | |
area += value; | |
} | |
for(j=i+1; j<row.length; j++) { | |
if (row[j] < value) break; | |
area += value; | |
} | |
} | |
if (area > largestArea) largestArea = area; | |
}) | |
return largestArea | |
} | |
// We test our helper function | |
let rowAreaTests = [ | |
[[0,0,0,0,0,0,1],1], | |
[[0,1,1,0,1,1,1],3], | |
[[0,1,2,2,4,0],6], | |
[[0,1,2,2,4,4],8] | |
] | |
rowAreaTests.map(([test, expected]) => largestRowArea(test) === expected ? "PASS" : "FAIL") | |
// OUTPUT: [ 'PASS', 'PASS', 'PASS', 'PASS' ] | |
// Calculate largest rectangular area in a matrix by first calculating the commulative row (with 0 zeroing out rows, and 1's | |
// being added to the previous total), then using the resulting row to caluculate the largestRow area, and returning the largest one of those | |
let getLargestRectangularArea = matrix => { | |
let largestArea = 0; | |
let currentAccumulatedRow = null; | |
matrix.forEach((row, rowIndex) => { | |
let intRow = row.map(s => +s); | |
if (currentAccumulatedRow) { | |
currentAccumulatedRow = currentAccumulatedRow.map((value, i) => intRow[i] ? value + intRow[i] : 0) | |
} else { | |
currentAccumulatedRow = [...intRow]; | |
} | |
let localLargestArea = largestRowArea(currentAccumulatedRow); | |
if (localLargestArea > largestArea) largestArea = localLargestArea; | |
}) | |
return largestArea; | |
} | |
// Tests of solution | |
let tests = [ | |
[[ | |
["1","1","1","0","0"], | |
["1","0","1","1","1"], | |
["1","1","0","1","1"], | |
["1","0","0","1","0"] | |
], 4], | |
[[ | |
["1","1","1","1","1"], | |
["1","0","1","1","1"], | |
["1","1","0","1","1"], | |
["1","0","0","1","0"] | |
], 6], | |
[[ | |
["1","1","1","1","1"], | |
["1","0","1","1","1"], | |
["1","1","1","1","1"], | |
["1","0","0","1","0"] | |
], 9], | |
[[ | |
["1","1","1","0","0"], | |
["1","1","1","1","1"], | |
["1","1","1","1","1"], | |
["1","0","0","1","0"] | |
], 10], | |
[[ | |
["0","0","0","0","0"], | |
["0","0","0","0","0"], | |
["0","0","0","0","0"], | |
["0","0","0","0","0"] | |
], 0], | |
[[ | |
["1","0","0","0","0"], | |
["0","0","0","0","0"], | |
["0","0","0","0","0"], | |
["0","0","0","0","0"] | |
], 1], | |
[[ | |
["1","1","1","1","1"], | |
["1","1","1","1","1"], | |
["1","1","1","1","1"], | |
["1","1","1","1","1"] | |
], 20], | |
] | |
tests.map(([test, expected]) => getLargestRectangularArea(test) === expected ? "PASS": "FAIL") | |
// OUTPUT: [ | |
// 'PASS', 'PASS', | |
// 'PASS', 'PASS', | |
// 'PASS', 'PASS', | |
// 'PASS' | |
//] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment