Leetcode 1401: Circle and Rectangle Overlapping

grid47
grid47
Exploring patterns and algorithms
Jun 19, 2024 5 min read

You are given a circle represented by its radius and center coordinates, as well as an axis-aligned rectangle represented by its bottom-left and top-right corners. Determine if the circle and the rectangle overlap, i.e., if there is any point inside both the circle and the rectangle.
Problem
Approach
Steps
Complexity
Input: The input consists of the radius and coordinates of the center of the circle, along with the coordinates of the bottom-left and top-right corners of the rectangle.
Example: [2, 1, 2, 0, 0, 3, 3]
Constraints:
• 1 <= radius <= 2000
• -10^4 <= xCenter, yCenter <= 10^4
• -10^4 <= x1 < x2 <= 10^4
• -10^4 <= y1 < y2 <= 10^4
Output: The output is a boolean value. Return `true` if the circle and rectangle overlap, otherwise return `false`.
Example: true
Constraints:
• The output is `true` if the circle and rectangle overlap, otherwise `false`.
Goal: The goal is to check if the circle and rectangle share any common points.
Steps:
• Shift the coordinates of the rectangle to the circle's center by subtracting the circle's center from the rectangle's coordinates.
• Check if any part of the rectangle intersects the circle by comparing the distance from the circle's center to the rectangle's edges and corners.
Goal: Ensure that all the input values are within the defined constraints.
Steps:
• The radius is a positive integer and the center coordinates are within a specified range.
• The rectangle's corners are within the allowed bounds.
Assumptions:
• The circle's radius is a positive integer.
• The rectangle's corners are distinct and follow the constraints.
Input: Input: [2, 1, 2, 0, 0, 3, 3]
Explanation: In this case, the circle with radius 2 and center (1, 2) intersects with the rectangle from (0, 0) to (3, 3), as the two shapes share a common point.

Input: Input: [2, 0, 0, 3, -1, 5, 1]
Explanation: Here, the circle does not overlap with the rectangle, as there is no point that lies in both the circle and the rectangle.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus