12
3
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000001111111111111100000000000000000011111111111111100000000000000000
0000001111111111111100000000000000000011111111111111100000000000000000
0000001111111111111100000000000000000011111111111111100000000000000000
0000001111111111111100000000000000000011111111111111100000000000000000
0000000000000000000000000000000000000011111111111111100000000000000000
0000000000000000000000000000000000000011111111111111100000000000000000
0000000000011111100000000000000000000011111111111111100000000000000000
0000000000011111100000000000000000000011111111111111100000000000000000
0000000000011111100000000000000000000011111111111111100000000000000000
0000000000000000000000000000000000000011111111111111100000000000000000
0000000000000000000000000000000000000011111111111111100000000000000000
0000000000000111111000000000000000000011111111111111100000000000000000
0000000000000100001000000111111000000011111111111111100000000010000000
0000000000000100001000000111111000000000000000000000011000000000000000
0000000000000111111000000111111000000000000000000000011000000000000000
0000000000000000000000000000111111000000000000000000000000000000000000
0000000000000000000000000000111111000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
Your are given a 2 dimensional array of bytes of size m x n. It is guaranteed that all the bytes are 1's or 0's. Find the number of rectangles represented by 1's when viewed in 2d, as shown above.
Only fully filled rectangles are considered for counting.
Rectangles must be surrounded by 0's unless they are on edge(1's diagonally touching rectangles are Okay though (see example.)).
For example, in above array there are 5 valid rectangles.
You can use any language.
1I think a better way to word it is to say that: rectangles must be surrounded by 0's, or an edge – Cruncher – 2014-01-16T16:43:27.987
Done. Thanks for wording it in a better English. – microbian – 2014-01-16T16:47:43.917
What about
1100\n1100\n0011\n0011
? – Cruncher – 2014-01-16T16:54:12.6271I think that's why I wrote 'adjacent / overlapping'. These are 2 valid rectangles from my initial intention. But the 'surrounding' condition is restricting them now. Do you have a better way to explain it – microbian – 2014-01-16T16:56:35.553
1Even at adjacent it's ambiguous whether or not diagonal means adjacent or not. The same ambiguity whether or not surrounded means, surrounded at the corners, or just sides – Cruncher – 2014-01-16T16:57:01.090
I'm not sure there is a more succinct way to word it. Probably keep it as is and explicitly state that the corners can be 1's. Or including that case in your example helps as well. – Cruncher – 2014-01-16T16:58:58.630
I put my ideas in an edit suggestion – Cruncher – 2014-01-16T17:02:57.630
Just for clarification, degenerate rectangles shouldn't be counted, correct? For example, are a single 1 or a single subrow/subcolumn of adjacent 1's invalid? – O-I – 2014-01-16T18:42:07.937
Yes, they are invalid, and should not be counted. – microbian – 2014-01-16T18:53:43.843
So far this question has got only 1 answer. The solution from Howard is pretty slick. It seems that it was on the harder side. – microbian – 2014-01-22T00:03:59.873