{"id":8734,"date":"2021-11-20T13:32:11","date_gmt":"2021-11-20T21:32:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8734"},"modified":"2021-11-20T13:32:48","modified_gmt":"2021-11-20T21:32:48","slug":"leetcode-2001-number-of-pairs-of-interchangeable-rectangles","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2001-number-of-pairs-of-interchangeable-rectangles\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2001. Number of Pairs of Interchangeable Rectangles"},"content":{"rendered":"\n<p>You are given&nbsp;<code>n<\/code>&nbsp;rectangles represented by a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>rectangles<\/code>, where&nbsp;<code>rectangles[i] = [width<sub>i<\/sub>, height<sub>i<\/sub>]<\/code>&nbsp;denotes the width and height of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;rectangle.<\/p>\n\n\n\n<p>Two rectangles&nbsp;<code>i<\/code>&nbsp;and&nbsp;<code>j<\/code>&nbsp;(<code>i &lt; j<\/code>) are considered&nbsp;<strong>interchangeable<\/strong>&nbsp;if they have the&nbsp;<strong>same<\/strong>&nbsp;width-to-height ratio. More formally, two rectangles are&nbsp;<strong>interchangeable<\/strong>&nbsp;if&nbsp;<code>width<sub>i<\/sub>\/height<sub>i<\/sub>&nbsp;== width<sub>j<\/sub>\/height<sub>j<\/sub><\/code>&nbsp;(using decimal division, not integer division).<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number<\/strong>&nbsp;of pairs of&nbsp;<strong>interchangeable<\/strong>&nbsp;rectangles in&nbsp;<\/em><code>rectangles<\/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 = [[4,8],[3,6],[10,20],[15,30]]\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The following are the interchangeable pairs of rectangles by index (0-indexed):\n- Rectangle 0 with rectangle 1: 4\/8 == 3\/6.\n- Rectangle 0 with rectangle 2: 4\/8 == 10\/20.\n- Rectangle 0 with rectangle 3: 4\/8 == 15\/30.\n- Rectangle 1 with rectangle 2: 3\/6 == 10\/20.\n- Rectangle 1 with rectangle 3: 3\/6 == 15\/30.\n- Rectangle 2 with rectangle 3: 10\/20 == 15\/30.\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 = [[4,5],[7,8]]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no interchangeable pairs of rectangles.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == rectangles.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>rectangles[i].length == 2<\/code><\/li><li><code>1 &lt;= width<sub>i<\/sub>, height<sub>i<\/sub>&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use aspect ratio as the key.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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  long long interchangeableRectangles(vector<vector<int>>& rectangles) {\n    long long ans  = 0;\n    unordered_map<long long, int> m;\n    for (const auto& r : rectangles) {\n      long long d = gcd(r[0], r[1]);\n      ans += m[((r[0] \/ d) << 20) | (r[1] \/ d)]++;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given&nbsp;n&nbsp;rectangles represented by a&nbsp;0-indexed&nbsp;2D integer array&nbsp;rectangles, where&nbsp;rectangles[i] = [widthi, heighti]&nbsp;denotes the width and height of the&nbsp;ith&nbsp;rectangle. Two rectangles&nbsp;i&nbsp;and&nbsp;j&nbsp;(i &lt; j) are considered&nbsp;interchangeable&nbsp;if they&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177],"class_list":["post-8734","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8734","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=8734"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8734\/revisions"}],"predecessor-version":[{"id":8736,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8734\/revisions\/8736"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}