bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 223. Rectangle Area

223. Rectangle Area

Leetcode Solutions

Calculating the Total Area of Two Rectilinear Rectangles

Algorithm

  1. Calculate the area of rectangle A (areaA) using (ax2 - ax1) * (ay2 - ay1).
  2. Calculate the area of rectangle B (areaB) using (bx2 - bx1) * (by2 - by1).
  3. Find the horizontal overlap (xOverlap) using max(0, min(ax2, bx2) - max(ax1, bx1)).
  4. Find the vertical overlap (yOverlap) using max(0, min(ay2, by2) - max(ay1, by1)).
  5. Calculate the overlap area (overlapArea) as xOverlap * yOverlap.
  6. Compute the total area by adding areaA and areaB and then subtracting overlapArea.
  7. Return the total area.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...