DEV Community

Paolo Ventura
Paolo Ventura

Posted on Edited on

100 algos in 100 days (Day 2)

Overlapping rectangles

These were my thoughts on the overlapping rectangles algo question on interview cake. https://www.interviewcake.com/question/javascript/rectangular-love?course=fc1&section=general-programming

Plan of attack

Bottom left is min y and X

Top right is max y and max X

A point is within the rectangle if it's y >= min y and y<=max y.

A point s X is within the rectangle if its X>= min X and X <= max X

Now check what section of rectangle is within

Find first point within
Or
Check outside

Find all points in rectangle
Iteration over the X and y fixed
Can skip some iterations if too low / too high

Potential Improvements

Find midpoint X and midpoint y
Can you do a kind of binary. Is it in left half.. in right half?

Might still need to cycle over all the points

Is it a ring thing
Are any of the points on the outside ring? Then incrementally check the inner rings.

Top comments (0)