Find the largest X-form in a binary matrix

I recently had a technical test for a company, and they had, in my opinion, a really interesting problem in recognizing a figure in a binary matrix.

The goal of the exercise was to create an algorithm that could find the largest form of X in a binary matrix and return its length. X is defined this way: -AX consists of two diagonals of equal length that have a unique point. For instance:

101
010
101

Contains a valid X of length 3, so the algorithm will return 3.

1001
0110
0110
1001

It does not contain any real X, so the algorithm will return 1, since the 1-length of X is single 1.

, , O (n3), . - , .

, ? , .

+4
4

O (n ^ 2) , , : \ , /. ( O (n ^ 2) , - O (n ^ 3), , \ /, -.) ; X , . ​​ , .

O (n ^ 2), .

+4

Althogh, , , , "X". , , .

, 45 , - . , x y.

. ( ).

, .

, -, , .

, , O (n ^ 2). m m + 2 . , , O (n ^ 2), , , O (n ^ 2).

, - O (n ^ 2), .

+1

O(m*n) , m - n :

. 1 . 1 , . . "children", . , X .

10001
01010
00100
01010
10001

:

p1   ...   p2
  p1 ... p2
   [p1,p2] // one cell with two parents
...
...

:

...
...
   [c1,c2]
  c1 ... c2
c1   ...   c2
+1

O (n) O (n) ( n ):

  • O (n) '1' NE, SE, SW NW ( ).

  • In O (n) time, find the cell that is β€œ1” and has the largest minimum of these four values. You can do this while installing the last of four.

  • The maximum length is more than double that indicated above.

0
source

Source: https://habr.com/ru/post/1680274/


All Articles