Counting rectangles in a grid
Difference between en3 and en4, changed 71 character(s)
Hi!↵

There's a problem I'd appreciate some help with (maybe hints or a sketch of solution...)↵

You have an `n \times n` grid, with `n \le 350`. Every element of the grid is either '.' or '#'.↵

The problem asks to count the number of rectangles made of '#', where sides are bigger than or equal to 3.↵

So the minimal rectangle would be↵

~~~~~↵
###↵
#.#↵
###↵
~~~~~↵

**IMPORTANT: There are 100 test cases, so the solution should be around O(N^2 lg N)**↵

I tried to count the complement: As the total number of rectangles in a grid is easy to compute, I tried to focus on every dot and count the number of rectangles it interrupted, but I haven't figured out exactly how to avoid counting things multiple times...↵

![ ](/predownloaded/84/16/8416a8652e2fdb482a5e37fab3377476275c14d0.png)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English bersub 2018-05-19 00:11:14 71 (published)
en3 English bersub 2018-05-19 00:10:29 2 (saved to drafts)
en2 English bersub 2018-05-18 23:30:26 502 (published)
en1 English bersub 2018-05-18 23:25:15 255 Initial revision (saved to drafts)