HomeLeetcode223. Rectangle Area - Leetcode Solutions

223. Rectangle Area – Leetcode Solutions

Description

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).

Examples:

Example 1:

Rectangle Area
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

Solution in Python

Python
class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, 
                    bx1: int, by1: int, bx2: int, by2: int) -> int:
        # Step 1: Calculate the area of both rectangles
        area1 = (ax2 - ax1) * (ay2 - ay1)  # Area of the first rectangle
        area2 = (bx2 - bx1) * (by2 - by1)  # Area of the second rectangle
        
        # Step 2: Calculate the overlap between the two rectangles
        overlap_x = max(0, min(ax2, bx2) - max(ax1, bx1))  # Overlap in x-axis
        overlap_y = max(0, min(ay2, by2) - max(ay1, by1))  # Overlap in y-axis
        overlap_area = overlap_x * overlap_y  # Area of the overlap
        
        # Step 3: Total area covered by both rectangles
        total_area = area1 + area2 - overlap_area
        
        return total_area

Explanation

  1. Area Calculation:
    • For each rectangle, the area is computed as the product of the width (difference between the x-coordinates) and the height (difference between the y-coordinates).
  2. Finding the Overlap:
    • The overlapping area is determined by finding the intersection of the x and y ranges of both rectangles. If the rectangles don’t overlap in either direction, the overlap is 0.
  3. Total Area:
    • The total area is calculated by adding the areas of both rectangles and subtracting the overlapping area (if any).

Time Complexity

  • O(1): This is a constant time solution since we are only performing a fixed number of arithmetic operations. The solution doesn’t depend on the input size because we’re given fixed values for the rectangle coordinates.

Solution in Javascript

JavaScript
var computeArea = function(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) {
    // Step 1: Calculate the area of both rectangles
    const area1 = (ax2 - ax1) * (ay2 - ay1); // Area of the first rectangle
    const area2 = (bx2 - bx1) * (by2 - by1); // Area of the second rectangle

    // Step 2: Calculate the overlap between the two rectangles
    // Overlap in the x-axis (horizontal)
    const overlap_x = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
    // Overlap in the y-axis (vertical)
    const overlap_y = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
    // Area of the overlap (if any)
    const overlapArea = overlap_x * overlap_y;

    // Step 3: Total area covered by both rectangles
    // Total area is the sum of the two rectangle areas minus the overlapping area
    return area1 + area2 - overlapArea;
};

Solution in Java

Java
class Solution {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, 
                           int bx1, int by1, int bx2, int by2) {
        // Step 1: Calculate the area of both rectangles
        int area1 = (ax2 - ax1) * (ay2 - ay1); // Area of the first rectangle
        int area2 = (bx2 - bx1) * (by2 - by1); // Area of the second rectangle
        
        // Step 2: Calculate the overlap between the two rectangles
        // Overlap in the x-axis (horizontal)
        int overlap_x = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
        // Overlap in the y-axis (vertical)
        int overlap_y = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
        // Area of the overlap (if any)
        int overlapArea = overlap_x * overlap_y;
        
        // Step 3: Total area covered by both rectangles
        // Total area is the sum of the two rectangle areas minus the overlapping area
        return area1 + area2 - overlapArea;
    }
}

Solution in C#

C#
public class Solution {
    public int ComputeArea(int ax1, int ay1, int ax2, int ay2, 
                           int bx1, int by1, int bx2, int by2) {
        // Step 1: Calculate the area of both rectangles
        int area1 = (ax2 - ax1) * (ay2 - ay1); // Area of the first rectangle
        int area2 = (bx2 - bx1) * (by2 - by1); // Area of the second rectangle
        
        // Step 2: Calculate the overlap in both x and y directions
        // Overlap in the x-axis (horizontal overlap)
        int overlap_x = Math.Max(0, Math.Min(ax2, bx2) - Math.Max(ax1, bx1));
        // Overlap in the y-axis (vertical overlap)
        int overlap_y = Math.Max(0, Math.Min(ay2, by2) - Math.Max(ay1, by1));
        // Overlap area (if any overlap exists)
        int overlapArea = overlap_x * overlap_y;
        
        // Step 3: Total area covered by both rectangles
        // Sum of the areas minus the overlap area
        return area1 + area2 - overlapArea;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular