1

The problem is in a 2d histogram with N columns, counting number of rectangles with area ≥ K. The columns have width 1 and I know the number of unit squares on the ith column.

I've come up with the following O(N²) algorithm: let hi be the height of the i th column. Then I can do the following: when I fix i,j as our bottom side of rectangle, I find the highest possible height of the rectangle h and add max(0, h - ceil(K/(j-i+1)) + 1) to the answer.

I heard there is an O(N log N) algorithm, and I tried to derive it by using the fact

Ni=1Ni  ~ N log N

However, that's all I have and I can't make further progress. Can you give a hint on the algorithm?

Kai Wang
  • 133
  • 5
  • Reframe the questions and remove $ signs. Tip - after posting the question try to read the question from reader's perspective – Liju John Jul 17 '20 at 03:23
  • 1
    Can you clarify? Do all columns have width 1? Are the heights integers? Is *K* an integer? I suppose that the rectangles to be counted, need their width and height to be integers. Any other constraints? – trincot Jul 17 '20 at 10:54
  • Here's my guess. The `NlogN` comes from sorting an array of `(height, index)` tuples. The first entry in the sorted array is a height (h1) that applies to the full width (w1) of the histogram. After counting rectangles within that (h1 x w1) area, split the histogram into two or more smaller histograms (essentially removing all columns of height h1). The next entry in the sorted array (that has height greater than h1) applies to exactly one of the smaller histograms. Count, split, continue. – user3386109 Jul 17 '20 at 19:39
  • When you say 2D histogram do you just mean a binary image? – orlp Jul 19 '20 at 00:28
  • @orlp [histogram](https://en.wikipedia.org/wiki/Histogram) – user3386109 Jul 19 '20 at 17:08

0 Answers0