Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
Example 1:
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 Output: 45
Example 2:
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2 Output: 16
Constraints:
-104 <= ax1 <= ax2 <= 104-104 <= ay1 <= ay2 <= 104-104 <= bx1 <= bx2 <= 104-104 <= by1 <= by2 <= 104Problem Overview: Two axis-aligned rectangles are defined by their bottom-left and top-right coordinates. The task is to compute the total area covered by both rectangles. If the rectangles overlap, the overlapping region must only be counted once.
Approach 1: Calculate Areas Separately and Subtract Overlap (O(1) time, O(1) space)
Each rectangle’s area is computed independently using the standard formula (x2 - x1) * (y2 - y1). Adding these two values double-counts any overlapping region. The fix is to compute the overlap width and height using coordinate comparisons: overlapWidth = max(0, min(ax2, bx2) - max(ax1, bx1)) and overlapHeight = max(0, min(ay2, by2) - max(ay1, by1)). Multiply them to get the overlapping area and subtract it from the sum of both rectangles. This solution relies purely on coordinate arithmetic, making it extremely efficient with constant O(1) time and O(1) space. The logic mainly uses geometric reasoning from Math and coordinate comparisons from Geometry.
The key insight is that overlap occurs only when projections on both the x-axis and y-axis intersect. Using min and max isolates the intersection boundaries. The max(0, ...) guard prevents negative values when rectangles do not intersect. This approach is compact, easy to implement, and handles all cases including disjoint rectangles, partial overlap, and one rectangle fully inside another.
Approach 2: Coordinate Sweep Method (O(1) time, O(1) space)
A coordinate sweep views the rectangles as vertical spans along the x-axis. First determine the combined x-intervals where rectangles exist. For each overlapping x segment, compute the active y coverage by intersecting the rectangles’ vertical ranges. The covered area becomes width * unionHeight. While sweep-line techniques usually appear in problems involving many rectangles, the same idea applies here conceptually with just two rectangles.
This approach emphasizes interval reasoning rather than direct subtraction. You determine horizontal overlap first, then compute the effective vertical span that contributes to area. Although the final complexity is still O(1), it mirrors how large-scale rectangle union problems are solved with sweep-line algorithms and interval merging. Understanding this method strengthens intuition for more advanced geometry problems involving multiple regions.
Recommended for interviews: The expected solution is the area calculation with overlap subtraction. Interviewers want to see you identify rectangle intersection using min/max comparisons and avoid negative overlap values. A sweep-style explanation demonstrates deeper geometric thinking, but the direct arithmetic approach shows the cleanest reasoning and implementation under time pressure.
This approach involves calculating the area of each rectangle separately, then finding the overlapping area, and subtracting it from the total. First, calculate the area of each rectangle by using the formula width multiplied by height. To find the overlap, determine the maximum of the bottom-left x and y coordinates, and the minimum of the top-right x and y coordinates. If the rectangles do intersect, subtract the overlap area from the sum of the rectangle areas to get the total covered area.
This program first computes the rectangles' areas using their width and height. It then calculates the overlap width and height based on intersecting limits of x and y coordinates. If these intersections are valid (width and height are positive), it then computes the overlap area and subtracts it from the total area of both rectangles. Finally, it outputs the total area covered by the rectangles.
The time complexity is O(1) since it involves basic arithmetic operations, and the space complexity is also O(1) as no additional space is needed beyond a few variables.
By using the coordinate sweep method, we can iterate through all points in a uniform grid covering both rectangles. This technique conceptualizes sweeping lines across the plane, adding and subtracting areas as intersections are counted. Use sorting and a plane-sweep technique to determine overlapping intervals more effectively.
This C variant checks if two rectangles can overlap by verifying bounding constraints. The rectangles' areas are computed, and the overlapping area is considered zero if there is no overlap. Otherwise, overlapping dimensions are calculated, and their areas are subtracted accordingly.
The time complexity remains O(1) since it involves constant time computation, while the space complexity is also O(1) because it uses a constant number of variables.
First, we calculate the area of the two rectangles separately, denoted as a and b. Then we calculate the overlapping width width and height height. The overlapping area is max(width, 0) times max(height, 0). Finally, we subtract the overlapping area from a and b.
The time complexity is O(1), and the space complexity is O(1).
| Approach | Complexity |
|---|---|
| Calculate Areas Separately and Subtract Overlap | The time complexity is O(1) since it involves basic arithmetic operations, and the space complexity is also O(1) as no additional space is needed beyond a few variables. |
| Coordinate Sweep Method | The time complexity remains O(1) since it involves constant time computation, while the space complexity is also O(1) because it uses a constant number of variables. |
| Calculate Overlapping Area | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Calculate Areas Separately and Subtract Overlap | O(1) | O(1) | Best general solution for two rectangles. Simple coordinate math and minimal implementation. |
| Coordinate Sweep Method | O(1) | O(1) | Useful for understanding how rectangle union problems scale to many rectangles using sweep-line techniques. |
leetcode 223 Rectangle Area • Codebix • 6,461 views views
Watch 9 more video solutions →Practice Rectangle Area with our built-in code editor and test cases.
Practice on FleetCode