Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} anishLearnsToCode / leetcode-algorithms Public Notifications You must be signed in to change notification settings Fork 17 Star 97 Code Issues 0 Pull requests 0 Discussions Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Discussions Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsleetcode-algorithms/src/RectangleArea.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory49 lines (41 loc) · 1.82 KB masterBreadcrumbsleetcode-algorithms/src/RectangleArea.javaCopy pathTopFile metadata and controlsCodeBlame49 lines (41 loc) · 1.82 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849// https://leetcode.com/problems/rectangle-area// T: O(1)// S: O(1) public class RectangleArea { private record Point(int x, int y) {} private static class Rectangle { public final int xl; public final int xr; public final int yb; public final int yt; public final int area; Rectangle(Point lowerLeft, Point upperRight) { xl = lowerLeft.x; xr = upperRight.x; yb = lowerLeft.y; yt = upperRight.y; area = (upperRight.x - lowerLeft.x) * (upperRight.y - lowerLeft.y); } public static Rectangle from(int x1, int y1, int x2, int y2) { final Point a1 = new Point(x1, y1); final Point a2 = new Point(x2, y2); return new Rectangle(a1, a2); } public Rectangle intersection(Rectangle other) { final int intersectionXl = Math.max(xl, other.xl); final int intersectionXr = Math.max(intersectionXl, Math.min(xr, other.xr)); final int intersectionYb = Math.max(yb, other.yb); final int intersectionYt = Math.max(intersectionYb, Math.min(yt, other.yt)); if (intersectionXl > intersectionXr || intersectionYb > intersectionYt) { return null; } return Rectangle.from(intersectionXl, intersectionYb, intersectionXr, intersectionYt); } } public static int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { final Rectangle rectangle1 = Rectangle.from(ax1, ay1, ax2, ay2); final Rectangle rectangle2 = Rectangle.from(bx1, by1, bx2, by2); final Rectangle intersection = rectangle1.intersection(rectangle2); return rectangle1.area + rectangle2.area - (intersection == null ? 0 : intersection.area); }} You can’t perform that action at this time.