{"id":7983,"date":"2021-01-17T12:39:55","date_gmt":"2021-01-17T20:39:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7983"},"modified":"2021-01-17T12:40:47","modified_gmt":"2021-01-17T20:40:47","slug":"leetcode-1725-number-of-rectangles-that-can-form-the-largest-square","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-1725-number-of-rectangles-that-can-form-the-largest-square\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1725. Number Of Rectangles That Can Form The Largest Square"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>rectangles<\/code>&nbsp;where&nbsp;<code>rectangles[i] = [l<sub>i<\/sub>, w<sub>i<\/sub>]<\/code>&nbsp;represents the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;rectangle of length&nbsp;<code>l<sub>i<\/sub><\/code>&nbsp;and width&nbsp;<code>w<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>You can cut the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;rectangle to form a square with a side length of&nbsp;<code>k<\/code>&nbsp;if both&nbsp;<code>k &lt;= l<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>k &lt;= w<sub>i<\/sub><\/code>. For example, if you have a rectangle&nbsp;<code>[4,6]<\/code>, you can cut it to get a square with a side length of at most&nbsp;<code>4<\/code>.<\/p>\n\n\n\n<p>Let&nbsp;<code>maxLen<\/code>&nbsp;be the side length of the&nbsp;<strong>largest<\/strong>&nbsp;square you can obtain from any of the given rectangles.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number<\/strong>&nbsp;of rectangles that can make a square with a side length of&nbsp;<\/em><code>maxLen<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rectangles = [[5,8],[3,9],[5,12],[16,5]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The largest squares you can get from each rectangle are of lengths [5,3,5,5].\nThe largest possible square is of length 5, and you can get it out of 3 rectangles.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> rectangles = [[2,3],[3,7],[4,3],[3,7]]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= rectangles.length &lt;= 1000<\/code><\/li><li><code>rectangles[i].length == 2<\/code><\/li><li><code>1 &lt;= l<sub>i<\/sub>, w<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>l<sub>i<\/sub>&nbsp;!= w<sub>i<\/sub><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Running Max of Shortest Edge<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countGoodRectangles(vector<vector<int>>& rectangles) {\n    int cur = 0;\n    int ans = 0;\n    for (const auto& r : rectangles) {\n      if (min(r[0], r[1]) > cur) {\n        cur = min(r[0], r[1]);\n        ans = 0;\n      }\n      if (min(r[0], r[1]) == cur) ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;rectangles&nbsp;where&nbsp;rectangles[i] = [li, wi]&nbsp;represents the&nbsp;ith&nbsp;rectangle of length&nbsp;li&nbsp;and width&nbsp;wi. You can cut the&nbsp;ith&nbsp;rectangle to form a square with a side length of&nbsp;k&nbsp;if&#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":[222,353],"class_list":["post-7983","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-easy","tag-gemoetry","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7983","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=7983"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7983\/revisions"}],"predecessor-version":[{"id":7985,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7983\/revisions\/7985"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}