#223
Medium Algorithms Rectangle area
Math Geometry
49.0% acceptance
Jan 12, 2026
2146
1668
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).
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn compute_area(ax1: i32, ay1: i32, ax2: i32, ay2: i32, bx1: i32, by1: i32, bx2: i32, by2: i32) -> i32 {
let area1 = (ax2 - ax1) * (ay2 - ay1);
let area2 = (bx2 - bx1) * (by2 - by1);
let overlap_width = (ax2.min(bx2) - ax1.max(bx1)).max(0);
let overlap_height = (ay2.min(by2) - ay1.max(by1)).max(0);
let overlap = overlap_width * overlap_height;
area1 + area2 - overlap
}
}