Solving Leetcode Interviews in Seconds with AI: Perfect Rectangle
Introduction
In this blog post, we will explore how to solve the LeetCode problem "391" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
Given an array rectangles where rectangles[i] = [xi, yi, ai, bi] represents an axis-aligned rectangle. The bottom-left point of the rectangle is (xi, yi) and the top-right point of it is (ai, bi). Return true if all the rectangles together form an exact cover of a rectangular region. Example 1: Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]] Output: true Explanation: All 5 rectangles together form an exact cover of a rectangular region. Example 2: Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]] Output: false Explanation: Because there is a gap between the two rectangular regions. Example 3: Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]] Output: false Explanation: Because two of the rectangles overlap with each other. Constraints: 1 <= rectangles.length <= 2 * 104 rectangles[i].length == 4 -105 <= xi < ai <= 105 -105 <= yi < bi <= 105
Explanation
Here's a breakdown of the approach, followed by the Python code:
- Area Check: Calculate the total area of all rectangles and the area of the bounding rectangle. If these areas don't match, it's not an exact cover.
- Corner Count: Maintain a count of how many times each corner point appears in the input rectangles. An exact cover requires each corner of the bounding rectangle to appear exactly once, and all other points within the bounding rectangle to appear an even number of times (0, 2, or 4) because they are internal corners of the rectangles.
Bounding Rectangle: Find the minimum and maximum x and y coordinates to define the bounding rectangle.
Runtime Complexity: O(N), where N is the number of rectangles. Storage Complexity: O(N)
Code
def isRectangleCover(rectangles):
"""
Checks if the given rectangles form an exact cover of a rectangular region.
Args:
rectangles: A list of lists, where each inner list represents a rectangle
in the format [x1, y1, x2, y2]. (x1, y1) is the bottom-left corner
and (x2, y2) is the top-right corner.
Returns:
True if the rectangles form an exact cover, False otherwise.
"""
if not rectangles:
return True # Empty input is considered a cover
area = 0
corners = {}
min_x = float('inf')
min_y = float('inf')
max_x = float('-inf')
max_y = float('-inf')
for x1, y1, x2, y2 in rectangles:
area += (x2 - x1) * (y2 - y1)
min_x = min(min_x, x1)
min_y = min(min_y, y1)
max_x = max(max_x, x2)
max_y = max(max_y, y2)
# Update corner counts
for x, y in [(x1, y1), (x1, y2), (x2, y1), (x2, y2)]:
if (x, y) in corners:
corners[(x, y)] += 1
else:
corners[(x, y)] = 1
expected_area = (max_x - min_x) * (max_y - min_y)
if area != expected_area:
return False
# Check that only the bounding rectangle corners appear once
bounding_corners = [(min_x, min_y), (min_x, max_y), (max_x, min_y), (max_x, max_y)]
for corner in bounding_corners:
if corner not in corners or corners[corner] != 1:
return False
# Check that all other corners appear an even number of times
for corner, count in corners.items():
if corner not in bounding_corners and count % 2 != 0:
return False
return True