{"id":4032,"date":"2018-09-19T00:24:55","date_gmt":"2018-09-19T07:24:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4032"},"modified":"2018-09-19T00:25:35","modified_gmt":"2018-09-19T07:25:35","slug":"leetcode-223-rectangle-area","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-223-rectangle-area\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 223. Rectangle Area"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Find the total area covered by two\u00a0<strong>rectilinear<\/strong>\u00a0rectangles in a\u00a0<strong>2D<\/strong>\u00a0plane.<\/p>\n<p>Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/rectangle_area.png\" alt=\"Rectangle Area\" \/><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>A = <span id=\"example-input-1-1\">-3<\/span>, B = <span id=\"example-input-1-2\">0<\/span>, C = <span id=\"example-input-1-3\">3<\/span>, D = <span id=\"example-input-1-4\">4<\/span>, E = <span id=\"example-input-1-5\">0<\/span>, F = <span id=\"example-input-1-6\">-1<\/span>, G = <span id=\"example-input-1-7\">9<\/span>, H = <span id=\"example-input-1-8\">2<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">45<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<p>Assume that the total area is never beyond the maximum possible value of\u00a0<strong>int<\/strong>.<\/p>\n<h1>Solution:<\/h1>\n<p>area1 + area2 &#8211; overlapped area<\/p>\n<p>Time complexity: O(1)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {\r\n    int area1 = (C - A) * (D - B);\r\n    int area2 = (G - E) * (H - F);\r\n    int x1 = max(A, E);\r\n    int x2 = max(x1, min(C, G));\r\n    int y1 = max(B, F);\r\n    int y2 = max(y1, min(D, H));\r\n    return area1 + area2 - (x2 - x1) * (y2 - y1);\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\n  public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {\r\n    int area1 = (C - A) * (D - B);\r\n    int area2 = (G - E) * (H - F);\r\n    int x1 = Math.max(A, E);\r\n    int x2 = Math.max(x1, Math.min(C, G));\r\n    int y1 = Math.max(B, F);\r\n    int y2 = Math.max(y1, Math.min(D, H));\r\n    return area1 + area2 - (x2 - x1) * (y2 - y1);\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\"># Author: Huahua\r\nclass Solution:\r\n  def computeArea(self, A, B, C, D, E, F, G, H):\r\n    area1 = (C - A) * (D - B);\r\n    area2 = (G - E) * (H - F);\r\n    x1 = max(A, E);\r\n    x2 = max(x1, min(C, G));\r\n    y1 = max(B, F);\r\n    y2 = max(y1, min(D, H));\r\n    return area1 + area2 - (x2 - x1) * (y2 - y1)<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Find the total area covered by two\u00a0rectilinear\u00a0rectangles in a\u00a02D\u00a0plane. Each rectangle is defined by its bottom left corner and top right corner as shown&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[178,284,31,177,343],"class_list":["post-4032","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-area","tag-geometry","tag-math","tag-medium","tag-rectangle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4032"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4032\/revisions"}],"predecessor-version":[{"id":4035,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4032\/revisions\/4035"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}