LeetCode 1401: Circle and Rectangle Overlapping : The Idea and the Intution
Geometry problems can look intimidating because they often make us think about equations, intersections, and many different cases.
LeetCode 1401 is actually much simpler once we ask the right question.
Instead of asking:
Which points of the circle intersect the rectangle?
Ask:
What is the closest point on the rectangle to the center of the circle?
Once we find that point, the rest is just the distance formula.
Understanding the Problem
We are given a circle:
Center: (xCenter, yCenter)
Radius: radiusAnd an axis-aligned rectangle:
Bottom-left: (x1, y1)
Top-right: (x2, y2)Visually:
(x2, y2)
┌───────────────┐
│ │
│ Rectangle │
│ │
└───────────────┘
(x1, y1)
.-----------.
.' '.
/ ● \
| (xCenter, |
| yCenter) |
\ /
'. .'
'-----------'We need to determine whether the two shapes share at least one point.
Touching also counts as overlapping.
The First Idea: Check Points on the Circle?
A natural first thought is to find points on the circle and check whether any of them are inside the rectangle.
The problem is simple:
A circle has infinitely many points.
We cannot practically test them one by one.
So we need to look at the problem from the opposite direction.
The Key Observation
Suppose we find the point in the rectangle that is closest to the center of the circle.
Call it:
(closestX, closestY)Then only two situations are possible.
Situation 1: The closest point is within the radius
Circle
.---------.
.' '.
/ ● \
| \ |
\ ●─────| Rectangle
'. .'|
'---------' |
|
└──────────● center
● closest rectangle pointThe rectangle has a point inside or on the circle.
Therefore:
Overlap: TrueSituation 2: Even the closest point is too far away
Circle.-------.
.' ● '.
/ \
'. .'
'-----' ┌──────────────┐
│ Rectangle │
│ │
└──────────────┘
If the closest rectangle point is farther away than the radius, every other rectangle point is also too far away.
Therefore:
Overlap: FalseThis is the entire idea behind the solution.
Step 1: Find the Closest X Coordinate
The rectangle extends horizontally from:
x1 to x2There are only three possible locations for xCenter.
Case 1: Center is to the left
Center
● ● |================|
x1 x2
↑
closestXThe closest x-coordinate is the left edge:
closestX = x1Case 2: Center is to the right
|================| ●
x1 x2 Center
↑
closestXThe closest x-coordinate is the right edge:
closestX = x2Case 3: Center is already between x1 and x2
|=========●=========|
x1 ↑ x2
xCenterThe center’s x-coordinate is already inside the rectangle’s horizontal range.
So:
closestX = xCenterThere is zero horizontal distance needed to reach the rectangle.
Putting the three cases together:
if xCenter < x1:
closestX = x1
elif xCenter > x2:
closestX = x2
else:
closestX = xCenterA simple way to remember this:
LEFT INSIDE RIGHT● |=====●=====| ●
|=======| |=======|
↑ ↑ ↑
x1 xCenter x2Use x1 Use xCenter Use x2
Step 2: Find the Closest Y Coordinate
The exact same reasoning works vertically.
The rectangle extends from:
y1 to y2If the circle center is below the rectangle:
closestY = y1If the circle center is above the rectangle:
closestY = y2If the circle center is already within the rectangle’s vertical range:
closestY = yCenterSo:
if yCenter < y1:
closestY = y1
elif yCenter > y2:
closestY = y2
else:
closestY = yCenterNow we have:
(closestX, closestY)This is the point in the rectangle closest to the center of the circle.
Step 3: Measure the Distance
We have two points:
Circle center:
(xCenter, yCenter)Closest rectangle point:
(closestX, closestY)First calculate their horizontal and vertical differences:
dx = closestX - xCenter
dy = closestY - yCenterVisually:
● Circle center
|\
| \
dy | \ distance
| \
|____● Closest point
dxThe distance formula is:
distance = sqrt(dx² + dy²)Then we could check:
distance <= radiusIf true, the circle reaches the rectangle.
Do We Need the Square Root?
No.
Get EverythingGuy’s stories in your inbox
Join Medium for free to get updates from this writer.
We could calculate:
sqrt(dx² + dy²) <= radiusBut we only care about comparing the distance with the radius.
We do not care about the actual distance value.
Because both sides are non-negative, we can square both sides:
dx² + dy² <= radius²So the code becomes:
dx**2 + dy**2 <= radius**2For example:
dx = 3
dy = 4
radius = 5Using the normal distance formula:
sqrt(3² + 4²)
= sqrt(9 + 16)
= sqrt(25)
= 55 <= 5: TrueUsing squared values:
3² + 4² <= 5²9 + 16 <= 2525 <= 25: True
Same answer, without calculating the square root.
Does the Subtraction Order Matter?
We used:
dx = closestX - xCenter
dy = closestY - yCenterCould we instead write:
dx = xCenter - closestX
dy = yCenter - closestYYes.
Suppose:
closestX = 3
xCenter = 7Then:
3 - 7 = -4
7 - 3 = 4But:
(-4)² = 16
4² = 16Since dx and dy are squared, the direction of subtraction does not matter.
Distance measures how far apart two points are, not which direction one point lies from the other.
One Python Detail to Remember
In Python:
** : exponentiation
^ : bitwise XORSo squaring a value is:
dx**2not:
dx^2This is an easy mistake to make in an interview.
Why the Algorithm Works
This is the most important part to understand.
We deliberately choose the rectangle point with the minimum distance from the circle’s center.
If:
closest distance <= radiusthen that point belongs to both the rectangle and the circle.
Therefore, they overlap.
If:
closest distance > radiusthen even the nearest rectangle point is outside the circle.
Every other rectangle point must be at least as far away.
Therefore, they cannot overlap.
That gives us the final condition:
dx**2 + dy**2 <= radius**2Final Algorithm
Circle center
↓
Find closestX using [x1, x2]
↓
Find closestY using [y1, y2]
↓
Closest rectangle point
(closestX, closestY)
↓
Calculate dx and dy
↓
dx² + dy² <= radius² ?
↓
YES NO
↓ ↓
True FalsePython Solution
class Solution:
def checkOverlap(self, radius: int, xCenter: int, yCenter: int,
x1: int, y1: int, x2: int, y2: int) -> bool:if xCenter < x1:
closestX = x1
elif xCenter > x2:
closestX = x2
else:
closestX = xCenter if yCenter < y1:
closestY = y1
elif yCenter > y2:
closestY = y2
else:
closestY = yCenter dx = closestX - xCenter
dy = closestY - yCenter if dx**2 + dy**2 <= radius**2:
return True
else:
return False
Complexity
Time Complexity: O(1)
There are no loops or recursive calls.
We perform only a fixed number of comparisons and arithmetic operations.
Space Complexity: O(1)
We only store a few variables:
closestX
closestY
dx
dyThe amount of extra memory does not depend on the input.
Final Takeaway
The hardest part of this problem is not the distance formula.
It is recognizing the right geometric question.
Do not ask:
Which point on the circle touches the rectangle?
Instead ask:
Which point on the rectangle is closest to the circle’s center?
Then the problem becomes:
Find closest point
↓
Measure its distance from center
↓
Compare with radiusIf the closest point is within or exactly on the radius, the shapes overlap.
That simple observation turns what looks like a complicated geometry problem into an O(1) solution.





