Monday, 9 March 2020

Algo: Find the largest square in a 1D array


var array = [

[0,0, 1, 1, 1],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 0, 0]

];

var finalAnswer = [];
function pushElm(arrayToPushInto, start, end, row) {
// debugger;


/* Case 1 */
if (start == end) {
return
}

const scoreRequired = end - start;

/* Case 6 */
if (row == 0) {
arrayToPushInto.push({ start, end, scoreRequired, row, lastCheckedRow: row });
return
}

let hasPushed = false;
for (let i = 0; i < arrayToPushInto.length; i++) {
const pushedRow = arrayToPushInto[i];

if (pushedRow.row !== row) {
const { start: pushedStart, end: pushedEnd, scoreRequired: pushedScoreRequired } = pushedRow;

/* Case 2 */
if (pushedStart == start && pushedEnd == end) {
hasPushed = true;
pushedRow.scoreRequired -= 1;
if (pushedRow.scoreRequired == 0) {
finalAnswer.push({ start, end, startRow: row - (scoreRequired), endRow: row, squareSize: end - start + 1 });
/* By not setting "lastCheckedRow", this row will be removed from further checks */
} else {
pushedRow.lastCheckedRow = row;
}


}

/* Case 3 */
else if (pushedStart <= start && pushedEnd >= end) {
hasPushed = true;
const pushedScoreGained = (pushedEnd - pushedStart) - pushedScoreRequired + 1;
const newRowToBePushed = { start, end, scoreRequired: end - start - pushedScoreGained, row, lastCheckedRow: row };
if (newRowToBePushed.scoreRequired <= 0) {
finalAnswer.push({ start, end, startRow: row - (scoreRequired), endRow: row, squareSize: end - start + 1 });
} else {
arrayToPushInto.push(newRowToBePushed);
}
}
/* Case 7 */
else if (start <= pushedStart && end >= pushedEnd) {
pushedRow.scoreRequired -= 1;
if (pushedRow.scoreRequired == 0) {
debugger;
finalAnswer.push({pushedStart, pushedEnd, startRow: row - (pushedEnd - pushedStart), endRow : row, squareSize: pushedEnd - pushedStart + 1});
} else {
pushedRow.lastCheckedRow = row;
}

}
}
}
/* Case 4 */
if (!hasPushed) {
arrayToPushInto.push({ start, end, scoreRequired: end - start, row, lastCheckedRow: row });
}
}

function cleanup(arrayToPushInto, lastCheckedRow) {
/* Check if scorerequired is not zero */
// debugger;
return arrayToPushInto.filter(arraySavedSet => arraySavedSet.lastCheckedRow === lastCheckedRow);
}

function getElm(array) {
var ans = [];
console.log('input array is ', array);
for (let i = 0; i < array.length; i++) {
const row = array[i];
let start;
let end;
for (let j = 0; j < row.length; j++) {
const bit = row[j];
if (bit == 1) {
if (start == undefined) {
start = j;
} else if (start != undefined && j == row.length - 1) {
end = j;
pushElm(ans, start, end, i);
start = null;
end = null;
}
} else if (bit == 0) {
if (start != undefined) {
end = j - 1;
pushElm(ans, start, end, i);
start = null;
end = null;
}
}
if (j == row.length - 1) {
ans = cleanup(ans, i);
}

}
}
let maxNo = -1;
let realFinalAns = null;
finalAnswer.forEach(ans=>{
if(ans.squareSize > maxNo) {
maxNo = ans.squareSize;
realFinalAns = ans;
}
})
console.log('real answer is ' , (realFinalAns));
}


getElm(array);



No comments:

Post a Comment