Input
====
A list of lists of letters like:
["B", "C", "C", "C", "C", "B", "B", "C", "A", "A"],
["B", "A", "C", "B", "B", "A", "B", "B", "A", "A"],
["B", "C", "B", "C", "A", "A", "A", "B", "C", "B"],
["B", "B", "B", "A", "C", "B", "A", "C", "B", "A"],
["A", "A", "A", "C", "A", "C", "C", "B", "A", "C"],
["A", "B", "B", "A", "A", "C", "B", "C", "C", "C"],
["C", "B", "A", "A", "C", "B", "B", "C", "A", "A"]
Output
====
The left-up and right-down positions of the largest rectangle corners:
> [1, 1, 8, 4]
Explanation:
===
A rectangle is valid if it has same "letter" on all corners. Sample rectangles:
[![enter image description here][1]][1]
This question is posted on Stack Overflow with title: [How to find the largest rectangle in a 2D array formed by four identical corners?][2] and with this rude JS solution (I can say "rude" because is my code ;) :
var l = [
["B", "C", "C", "C", "C", "B", "B", "C", "A", "A"],
["B", "A", "C", "B", "B", "A", "B", "B", "A", "A"],
["B", "C", "B", "C", "A", "A", "A", "B", "C", "B"],
["B", "B", "B", "A", "C", "B", "A", "C", "B", "A"],
["A", "A", "A", "C", "A", "C", "C", "B", "A", "C"],
["A", "B", "B", "A", "A", "C", "B", "C", "C", "C"],
["C", "B", "A", "A", "C", "B", "B", "C", "A", "A"]];
var squares = [];
//for each position
for (var column=0; column< l[0].length; column++)
for (var row=0; row< l.length ; row++ )
//look for all scuares from this position:
for (var next_column = column+1; next_column < l[0].length; next_column++ )
if ( l[row][next_column] == l[row][column] )
for (var next_row = row+1; next_row < l.length; next_row++)
//if it is a square
if (l[next_row][column] == l[row][column] &&
l[next_row][next_column] == l[row][column])
//annotate square
squares.push( [ column, row, next_column, next_row ] );
//get areas from squares
var area_l = squares.map(function(an_square) {
return (an_square[2]-an_square[0])*(an_square[3]-an_square[1]);
});
//search for big area
var max_area_index = area_l.indexOf(Math.max( ...area_l ));
//here it is
console.log( squares[max_area_index] );
Ok, is my first post, be tolerant with me please. I will change all you say to improve the quiz.
[1]: https://i.sstatic.net/ZIkkF.png
[2]: https://stackoverflow.com/questions/49708412/how-to-find-the-largest-rectangle-in-a-2d-array-formed-by-four-identical-corners