Solution For Rectangle Area
The Rectangle Area problem on LeetCode is a mathematical problem that involves finding the area of two overlapping rectangles.
The problem statement reads as follows:
“Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner coordinates.”
The input is given in the form of four integers: the coordinates of the bottom left corner and top right corner of the two rectangles. The output is the total area covered by the two rectangles.
Example:
Input:
rectangle1 = [-3, 0, 3, 4]
rectangle2 = [0, -1, 9, 2]
Output:
45
To solve this problem, the first step is to calculate the area of each rectangle separately. This can be done by finding the difference between the x-coordinates and y-coordinates of the bottom left and top right corners of each rectangle.
Next, we need to check if the two rectangles overlap. To do this, we need to compare the x-coordinates and y-coordinates of the rectangles and see if they intersect.
If the rectangles intersect, we need to find the area of the intersection. This can be done by finding the x-coordinate and y-coordinate of the bottom left corner and top right corner of the intersection rectangle.
Now, we can calculate the total area covered by the two rectangles by subtracting the area of the intersection rectangle from the sum of the areas of the two rectangles.
Here is the Python code to solve the Rectangle Area problem on LeetCode:
“`
class Solution:
def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int:
# Calculate the area of each rectangle
area1 = (C – A) * (D – B)
area2 = (G – E) * (H – F)
# Check if the two rectangles intersect
x_overlap = max(0, min(C, G) - max(A, E))
y_overlap = max(0, min(D, H) - max(B, F))
overlap_area = x_overlap * y_overlap
# Calculate the total area covered by the two rectangles
total_area = area1 + area2 - overlap_area
return total_area
“`
In the above code, we define a class called Solution and a method called computeArea. The method takes in the coordinates of the two rectangles as input and returns the total area covered by the two rectangles.
First, we calculate the area of each rectangle separately. Next, we calculate the overlap area by finding the x-coordinate and y-coordinate of the intersection rectangle. We use the max and min functions to find the x-coordinate and y-coordinate of the bottom left and top right corners of the intersection rectangle.
Finally, we calculate the total area covered by the two rectangles by subtracting the overlap area from the sum of the areas of the two rectangles. We return the total area as output.
Step by Step Implementation For Rectangle Area
/* Calculate the area of a rectangle, given its width and height. */ public int rectangleArea(int width, int height) { // calculate the area of the rectangle return width * height; }
class Solution: def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: #calculate the area of the first rectangle area1 = (C-A) * (D-B) #calculate the area of the second rectangle area2 = (G-E) * (H-F) #calculate the overlap between the two rectangles overlap = self.overlap(A, B, C, D, E, F, G, H) #calculate and return the total area return area1 + area2 - overlap def overlap(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: #calculate the width and height of the overlap width = self.overlapDimension(A, C, E, G) height = self.overlapDimension(B, D, F, H) #calculate and return the area of the overlap return width * height def overlapDimension(self, a: int, c: int, e: int, g: int) -> int: #if the rectangles do not overlap in this dimension, return 0 if a >= g or c <= e: return 0 #otherwise, calculate and return the dimension of the overlap else: return min(c, g) - max(a, e)
/** * @param {number} A * @param {number} B * @param {number} C * @param {number} D * @param {number} E * @param {number} F * @param {number} G * @param {number} H * @return {number} */ var computeArea = function(A, B, C, D, E, F, G, H) { //find the total area of both rectangles var totalArea = (C-A)*(D-B) + (G-E)*(H-F); //find the overlapping area var xOverlap = Math.max(0, Math.min(C,G) - Math.max(A,E)); var yOverlap = Math.max(0, Math.min(D,H) - Math.max(B,F)); var overlap = xOverlap * yOverlap; //subtract the overlapping area from the total area to get the final answer return totalArea - overlap; };
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { //calculate the area of first rectangle int area1 = (C-A) * (D-B); //calculate the area of second rectangle int area2 = (G-E) * (H-F); //calculate the length of overlapping area int length = 0; if(E>=A && E<=C){ length = min(C,G) - E; } else if(A>=E && A<=G){ length = min(C,G) - A; } //calculate the width of overlapping area int width = 0; if(F>=B && F<=D){ width = min(D,H) - F; } else if(B>=F && B<=H){ width = min(D,H) - B; } //calculate the area of overlapping rectangle int area = length * width; //calculate the total area return area1 + area2 - area; } };
using System; public class Solution { public int ComputeArea(int A, int B, int C, int D, int E, int F, int G, int H) { //Compute the area of rectangle 1 int rect1 = (C-A) * (D-B); //Compute the area of rectangle 2 int rect2 = (G-E) * (H-F); //Compute the area of the intersection int intersect = Math.Min(C,G) - Math.Max(A,E) * Math.Min(D,H) - Math.Max(B,F); //Return the sum of the areas of the two rectangles, minus the area of the intersection return rect1 + rect2 - intersect; } }