{"id":7348,"date":"2020-09-05T22:05:47","date_gmt":"2020-09-06T05:05:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7348"},"modified":"2020-09-05T22:06:13","modified_gmt":"2020-09-06T05:06:13","slug":"leetcode-1577-number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1577-number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers"},"content":{"rendered":"\n<p>Given two arrays of integers&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>, return the number of triplets formed (type 1 and type 2) under the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Type 1: Triplet (i, j, k)&nbsp;if&nbsp;<code>nums1[i]<sup>2<\/sup>&nbsp;== nums2[j] * nums2[k]<\/code>&nbsp;where&nbsp;<code>0 &lt;= i &lt; nums1.length<\/code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; k &lt; nums2.length<\/code>.<\/li><li>Type 2:&nbsp;Triplet (i, j, k) if&nbsp;<code>nums2[i]<sup>2<\/sup>&nbsp;== nums1[j] * nums1[k]<\/code>&nbsp;where&nbsp;<code>0 &lt;= i &lt; nums2.length<\/code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; k &lt; nums1.length<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [7,4], nums2 = [5,2,8,9]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8). \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> nums1 = [1,1], nums2 = [1,1,1]\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong> All Triplets are valid, because 1^2 = 1 * 1.\nType 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2).  nums1[i]^2 = nums2[j] * nums2[k].\nType 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [7,7,8,3], nums2 = [1,2,9,7]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 2 valid triplets.\nType 1: (3,0,2).  nums1[3]^2 = nums2[0] * nums2[2].\nType 2: (3,0,1).  nums2[3]^2 = nums1[0] * nums1[1].\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no valid triplets.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>For each number y in the second array, count its frequency.<\/p>\n\n\n\n<p>For each number x in the first, if x * x % y == 0, let r = x * x \/ y<br>if r == y: ans += f[y] * f[y-1]<br>else ans += f[y] * f[r]<br><br>Final ans \/= 2<\/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  int numTriplets(vector<int>& nums1, vector<int>& nums2) {\n    return solve(nums1, nums2) + solve(nums2, nums1);\n  }\nprivate:\n  int solve(vector<int>& nums1, vector<int>& nums2) {\n    int ans = 0;\n    unordered_map<int, int> f;\n    for (int y : nums2) ++f[y];\n    for (long x : nums1)\n      for (const auto& [y, c] : f) {                \n        long r = x * x \/ y;\n        if (x * x % y || !f.count(r)) continue;\n        if (r == y) ans += c * (c - 1);\n        else ans += c * f[r];\n      }\n    return ans \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two arrays of integers&nbsp;nums1&nbsp;and&nbsp;nums2, return the number of triplets formed (type 1 and type 2) under the following rules: Type 1: Triplet (i, j,&#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":[122,24,82,177],"class_list":["post-7348","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-combination","tag-frequency","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7348","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=7348"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7348\/revisions"}],"predecessor-version":[{"id":7350,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7348\/revisions\/7350"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}