↓Skip to main content
  1. LeetCode/

LeetCode 1401: Circle and Rectangle Overlapping

·2 mins· ·
LeetCode Medium Math Geometry
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-05

  • Submissions: Wrong Answer ×3 → Accepted

Approach
#

The circle and the rectangle intersect ⇔ the rectangle’s nearest point to the center lies in the circle.

That point is easy: x and y are independent, so clamp the center’s x into [x1, x2] and y into [y1, y2]:

nx = min(max(xc, x1), x2)
ny = min(max(yc, y1), y2)

With the center inside the rectangle, the nearest point is the center itself at distance 0, which also counts as overlapping.

Compare squared distances to avoid floating-point square roots.

My Attempts
#

My version split into four directions (the center left of, below, right of, or above the rectangle) and found the nearest point in each. Two of the four ifs were wrong:

VersionProblem
Attempt 1Missed the center-inside case; the last direction (above) was yCenter < y2 instead of yCenter > y2. With radius 2, center (6, 4) and rectangle (5, 0)–(6, 2), the top edge point (6, 2) is exactly 2 away, so the answer is True; it returned False
Attempt 2Added center-inside, but the “above” condition was still wrong; the same example returned False
Attempt 3Changed it to <=, still the wrong direction. With radius 3, center (3, 4) and rectangle (2, 2)–(5, 3), the answer is True; it returned False
ACChanged it to yCenter >= y2

Clamping needs no cases, so there is no direction to get backwards.

Solution
#

class Solution:
    def checkOverlap(self, radius: int, xCenter: int, yCenter: int, x1: int, y1: int, x2: int, y2: int) -> bool:
        nx = min(max(xCenter, x1), x2)             # the rectangle's nearest point to the center
        ny = min(max(yCenter, y1), y2)
        return (nx - xCenter) ** 2 + (ny - yCenter) ** 2 <= radius ** 2

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Writing a comparison backwards in a case split.
  • Missing the center-inside case.
  • Comparing floating-point square roots: squared comparisons are more robust.

Interview Explanation
#

I find the rectangle’s nearest point to the center by clamping each coordinate of the center into the rectangle; they overlap when its squared distance is at most the squared radius. With the center inside, the nearest point is the center itself, which works naturally. Time O(1).

Related

LeetCode 973: K Closest Points to Origin
·1 min
LeetCode Medium Array Math Divide-and-Conquer Geometry
LeetCode 973 note: as in Top-K, keep a size-k max-heap (negated distances) holding the k closest points
LeetCode 3648: Minimum Sensors to Cover Grid
·2 mins
LeetCode Medium Math
LeetCode 3648 note: each sensor covers a (2k + 1) × (2k + 1) square, so take a ceiling division in each direction and multiply
LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 1814: Count Nice Pairs in an Array
·1 min
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 note: rearrange the condition into nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]), count pairs per value, and remember the modulus
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front