{"id":4297,"date":"2018-11-11T15:49:06","date_gmt":"2018-11-11T23:49:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4297"},"modified":"2018-11-13T00:23:00","modified_gmt":"2018-11-13T08:23:00","slug":"leetcode-939-minimum-area-rectangle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-939-minimum-area-rectangle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 939. Minimum Area Rectangle"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.<\/p>\n<p>If there isn&#8217;t any rectangle, return 0.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[[1,1],[1,3],[3,1],[3,3],[2,2]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">4<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">2<\/span>\r\n<\/pre>\n<p><strong>Note<\/strong>:<\/p>\n<ol>\n<li><code>1 &lt;= points.length &lt;= 500<\/code><\/li>\n<li><code>0 &lt;=\u00a0points[i][0] &lt;=\u00a040000<\/code><\/li>\n<li><code>0 &lt;=\u00a0points[i][1] &lt;=\u00a040000<\/code><\/li>\n<li>All points are distinct.<\/li>\n<\/ol>\n<h1>Solution 1: HashTable + Brute Force<\/h1>\n<p>Try all pairs of points to form a diagonal and see whether pointers of another diagonal exist or not.<\/p>\n<p>Assume two points are (x0, y0), (x1, y1) x0 != x1 and y0 != y1. The other two points will be (x0, y1), (x1, y0)<\/p>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/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, running time: 96 ms\r\nclass Solution {\r\npublic:\r\n  int minAreaRect(vector&lt;vector&lt;int&gt;&gt;&amp; points) {    \r\n    unordered_map&lt;int, unordered_set&lt;int&gt;&gt; s;\r\n    for (const auto&amp; point : points)\r\n      s[point[0]].insert(point[1]);\r\n    \r\n    const int n = points.size();\r\n    int min_area = INT_MAX;\r\n    for (int i = 0; i &lt; n; ++i)\r\n      for (int j = i + 1; j &lt; n; ++j) {\r\n        int x0 = points[i][0];\r\n        int y0 = points[i][1];\r\n        int x1 = points[j][0];\r\n        int y1 = points[j][1];\r\n        if (x0 == x1 || y0 == y1) continue;\r\n        int area = abs(x0 - x1) * abs(y0 - y1);\r\n        if (area &gt; min_area) continue;\r\n        if (!s[x1].count(y0) || !s[x0].count(y1)) continue;\r\n        min_area = area;\r\n      }\r\n    return min_area == INT_MAX ? 0 : min_area;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ \/ 1D hashtable<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua, 68 ms\r\nclass Solution {\r\npublic:\r\n  int minAreaRect(vector&lt;vector&lt;int&gt;&gt;&amp; points) {    \r\n    unordered_set&lt;int&gt; s;\r\n    for (const auto&amp; point : points)\r\n      s.insert((point[0] &lt;&lt; 16) | point[1]);\r\n    \r\n    const int n = points.size();\r\n    int min_area = INT_MAX;\r\n    for (int i = 0; i &lt; n; ++i)\r\n      for (int j = i + 1; j &lt; n; ++j) {\r\n        int x0 = points[i][0];\r\n        int y0 = points[i][1];\r\n        int x1 = points[j][0];\r\n        int y1 = points[j][1];\r\n        if (x0 == x1 || y0 == y1) continue;\r\n        int area = abs(x0 - x1) * abs(y0 - y1);\r\n        if (area &gt; min_area) continue;\r\n        int p0 = (x1 &lt;&lt; 16) | y0;\r\n        int p1 = (x0 &lt;&lt; 16) | y1;\r\n        if (!s.count(p0) || !s.count(p1)) continue;\r\n        min_area = area;\r\n      }\r\n    return min_area == INT_MAX ? 0 : min_area;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the&#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":[284,82,31,343],"class_list":["post-4297","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-geometry","tag-hashtable","tag-math","tag-rectangle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4297","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=4297"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4297\/revisions"}],"predecessor-version":[{"id":4306,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4297\/revisions\/4306"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}