223. Rectangle Area Leetcode Solution
Rectangle Area Leetcode Problem :
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).
Rectangle Area Leetcode Solution :
Constraints :
- -10^4 <= ax1 <= ax2 <= 10^4
- -10^4 <= ay1 <= ay2 <= 10^4
- -10^4 <= bx1 <= bx2 <= 10^4
- -10^4 <= by1 <= by2 <= 10^4
Example 1:
- Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
- Output: 16
Intuition :
Intuition of this problem is simple. To calculate the common area between two rectangles,we use (area_1 + area_2 – common_area(between two rectangles)).
Approach :
Firstly,we will calculate the area1 and area2 by simple mathematics .i.e. length * width .Now our main task is compute the common area.There are few cases that can exist between two rectangles that are as follows:
Case 1: Rectangle 1 is enclosed in rectangle 2.
- Condition: ax1>=bx1 && ax2<=bx2 && ay1>=by1 && ay2<=by2
- Common Area:Area 1
Case 2: Rectangle 2 is enclosed in rectangle 1.
- Condition: bx1>=ax1 && bx2<=ax2 && by1>=ay1 && by2<=ay2
- Common Area:Area 2
Case 3: Rectangle 1 and rectangle 2 share some common area.
- Condition: max(ax1,bx1)<= min(ax2,bx2) && max(ay1,by1)<= min(ay2,by2)
- common_area= (max(ax1,bx1)- min(ax2,bx2))*(max(ay1,by1)- min(ay2,by2))
Case 4: Rectangle 1 and rectangle 2 has no common area.
- Common area=0
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code :
class Solution { public: int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { long long area1=(ax2-ax1)*(ay2-ay1); long long area2=(bx2-bx1)*(by2-by1); long long common_area; if( ax1>=bx1 && ax2<=bx2 && ay1>=by1 && ay2<=by2){ common_area= area1; } else if( bx1>=ax1 && bx2<=ax2 && by1>=ay1 && by2<=ay2){ common_area= area2; } else if( max(ax1,bx1)<= min(ax2,bx2) && max(ay1,by1)<= min(ay2,by2)){ common_area= (max(ax1,bx1)- min(ax2,bx2))*(max(ay1,by1)- min(ay2,by2)); } else common_area=0; return area1+area2-common_area; } };
class Solution { public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { if(ax2< bx1||bx2< ax1 ) return (bx2-bx1)*(by2-by1) + (ax2-ax1)*(ay2-ay1); if(ay2< by1 || by2< ay1) return (bx2-bx1)*(by2-by1) + (ax2-ax1)*(ay2-ay1); int right = Math.min(ax2,bx2); int left = Math.max(ax1,bx1); int top = Math.min(by2,ay2); int bottom = Math.max(by1,ay1); return (bx2-bx1)*(by2-by1) + (ax2-ax1)*(ay2-ay1) - (right-left)*(top-bottom); } }
class Solution: def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int: # Function to caculate area of rectangle (Length * width) def area(x1,y1,x2,y2): return (x2-x1)*(y2-y1) # Finding the overlap rectangle length and width overlapX = max(min(ax2,bx2)-max(ax1,bx1), 0) overlapY = max(min(ay2,by2)-max(ay1,by1), 0) # Area1 + Area2 - Overlap Rectangle Area return area(ax1,ay1,ax2,ay2) + area(bx1,by1,bx2,by2) - overlapX * overlapY
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment