Advertisement

Rectangle Area - LeetCode 223 Solution

Rectangle Area - Complete Solution Guide

Rectangle Area is LeetCode problem 223, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to calculate the total area covered by two rectangles in a 2D plane. We are given the coordinates of the bottom-left and top-right corners of each rectangle. The goal is to find the combined area, taking into account any overlapping region, so we don't count it twice. Input consists of eight integers representing the x and y coordinates of the two rectangles: (ax1, ay1, ax2, ay2) and (bx1, by1, bx2, by2). The output is a single integer representing the total area.

Solution Approach

The solution calculates the area of each rectangle separately. Then, it determines the overlapping area, if any exists. The total area is computed by summing the individual areas and subtracting the overlapping area to avoid double-counting. The overlap is calculated by finding the intersection of the x and y projections of the rectangles.

Step-by-Step Algorithm

  1. Step 1: Calculate the area of the first rectangle (area1) as (ax2 - ax1) * (ay2 - ay1).
  2. Step 2: Calculate the area of the second rectangle (area2) as (bx2 - bx1) * (by2 - by1).
  3. Step 3: Determine the x-coordinate of the left edge of the overlapping rectangle by taking the maximum of ax1 and bx1.
  4. Step 4: Determine the x-coordinate of the right edge of the overlapping rectangle by taking the minimum of ax2 and bx2.
  5. Step 5: Determine the y-coordinate of the bottom edge of the overlapping rectangle by taking the maximum of ay1 and by1.
  6. Step 6: Determine the y-coordinate of the top edge of the overlapping rectangle by taking the minimum of ay2 and by2.
  7. Step 7: Calculate the width of the overlapping rectangle as the difference between the right and left x-coordinates. If the width is negative (meaning no overlap in the x-dimension), set it to 0.
  8. Step 8: Calculate the height of the overlapping rectangle as the difference between the top and bottom y-coordinates. If the height is negative (meaning no overlap in the y-dimension), set it to 0.
  9. Step 9: Calculate the area of the overlapping rectangle as the product of its width and height.
  10. Step 10: Return the total area as area1 + area2 - overlap_area.

Key Insights

  • Insight 1: The core challenge is correctly identifying and calculating the overlapping area between the two rectangles.
  • Insight 2: To find the overlap, we need to find the intersection of the x-ranges and y-ranges of the two rectangles.
  • Insight 3: We must handle the case where there is no overlap; in this case, the overlap area should be zero.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Math, Geometry.

Companies

Asked at: Nvidia.