26
3
Input
The board: A 2D container (matrix, list of lists, etc.) 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"]
If you choose a list of lists you may assume that all of the sublists are of the same length.
Rules
- To make a valid rectangle you need all rectangle corners with the same 'letter'.
- Example, look the sample board with X bellow. You can see 'X' on (1,0) also on (4,0) also on ( 1,3) and on (4,3) then you have the rectange [1,0,4,3] that means from (1,0) to (4,3):
Sample board with X:
["B", "X", "C", "C", "X", "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", "X", "B", "A", "X", "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"]
- The goal is to find the rectangle or one of the rectangles with the largest area, calculated by (right-left+1)*(bottom-top+1)
- If there are multiple rectangles with the same maximum area, output any one. Optionally the one with (top coordinate, left coordinate, right coordinate, bottom coordinate) lexicographically smallest.
- Rectangles must have edges parallel to the board's edge.
- Each letter is a printable ASCII char from A to Z (both included).
Output
The output should be the left-up and right-down positions of the largest area rectangle corners. For the first sample "board" the big square is the yellow one:
And the answer should be:
[1, 1, 8, 4]
A second example test case
An input of:
["C", "D", "D", "D", "A", "A"],
["B", "D", "C", "D", "A", "A"],
["B", "D", "D", "C", "A", "C"],
["B", "D", "B", "C", "A", "C"]
Should yield one of these three coordinate lists identifying an area six rectangles:
[1, 0, 2, 2]
[1, 0, 3, 1]
[3, 2, 5, 3]
This question is posted on Stack Overflow with title: How to find the largest rectangle in a 2D array formed by four identical corners? and with this rude JS solution (I can say "rude" because is my code ;) :
Ok, is my first post, be tolerant with me please. I will change all you say to improve the quiz.
7Hi, welcome to PPCG! This seems to be a good challenge, but seems to lack any winning criterion. Typically, posts here are tagged [code-golf], which means that the shortest code (in bytes) wins. – Conor O'Brien – 2018-04-07T16:11:50.363
1
I thought I would let you know that we have a sandbox that you can use to get feedback on questions before they are posted to the main site. The sandbox is useful to pretty much everyone here but especially to beginners, who might not know all the rules and expectations we have.
– Post Rock Garf Hunter – 2018-04-07T16:48:14.680@Arnauld, I ask for help to fix English but I guess the semantic was changed. Can you fix it? – danihp – 2018-04-07T17:25:44.580
May we use 1-indexing rather than 0 (so long as it is consistent)? – Jonathan Allan – 2018-04-07T18:08:26.413
2Some answers output the coordinates in sorting order for the "first" rectangle (i.e, top, left, bottom, right) instead of (left, top, right, bottom) as seen in your examples. Is this ok? – nimi – 2018-04-07T18:58:27.467
2Less strict output formats usually encourage more answers, so something like
((left,top),(right,bottom))
should be fine too. I deleted my answer and answer again when the question is completely refined. – Angs – 2018-04-07T19:10:14.213I'd suggest a test like
["A","A","B","C","D","E","F"],["G","H","I","J","K","L","G"],["A","A","M","N","O","P","Q"]
->[0,1,6,1]
(the twoG
making an area of seven vs the fourA
making an area of six) – Jonathan Allan – 2018-04-07T20:18:46.843So now each individual letter counts as a rectangle of area 1 if all else fails? – Neil – 2018-04-07T20:30:27.660
@Neil, yes. Looks more elegant like this. – danihp – 2018-04-07T20:42:56.260
@user56656, all answers are awesome, I've read 'What's the “accepted answer”?' but I still have doubts. Can you explain to me which is usually the criteria to 'accept ans answer'? Are you so kind to help to me in this subject? I would like to avoid any injustice.
– danihp – 2018-04-11T07:33:49.7401
Sure, If you are going to accept an answer it should be the shortest overall, this is how most people like things done on the site. However there is no consequence for not doing so. There is also a growing opinion that accepting answers is detrimental to the site. I am of that opinion, and thus I never accept answers on my challenges. What you do is up to you.
– Post Rock Garf Hunter – 2018-04-11T14:31:03.817Hi @user56656, thanks about explanation. I agree all your comment. – danihp – 2018-04-11T14:41:26.987