{"id":2347,"date":"2018-03-24T12:11:52","date_gmt":"2018-03-24T19:11:52","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2347"},"modified":"2018-03-24T12:32:19","modified_gmt":"2018-03-24T19:32:19","slug":"leetcode-447-number-of-boomerangs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-447-number-of-boomerangs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 447. Number of Boomerangs"},"content":{"rendered":"<h1>Problem<\/h1>\n<div class=\"question-description\">\n<div>\n<p>Given\u00a0<i>n<\/i>\u00a0points in the plane that are all pairwise distinct, a &#8220;boomerang&#8221; is a tuple of points\u00a0<code>(i, j, k)<\/code>\u00a0such that the distance between\u00a0<code>i<\/code>\u00a0and\u00a0<code>j<\/code>\u00a0equals the distance between\u00a0<code>i<\/code>\u00a0and\u00a0<code>k<\/code>\u00a0(<b>the order of the tuple matters<\/b>).<\/p>\n<p>Find the number of boomerangs. You may assume that\u00a0<i>n<\/i>\u00a0will be at most\u00a0<b>500<\/b>\u00a0and coordinates of points are all in the range\u00a0<b>[-10000, 10000]<\/b>\u00a0(inclusive).<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>\r\n[[0,0],[1,0],[2,0]]\r\n\r\n<b>Output:<\/b>\r\n2\r\n\r\n<b>Explanation:<\/b>\r\nThe two boomerangs are <b>[[1,0],[0,0],[2,0]]<\/b> and <b>[[1,0],[2,0],[0,0]]<\/b>\r\n<\/pre>\n<\/div>\n<\/div>\n<h1><strong>Solution: HashTable<\/strong><\/h1>\n<p>For each point, compute the distance to the rest of the points and count.<\/p>\n<p>if there are k points that have the same distance to current point, then there are P(k,2) = k*k-1 Boomerangs.<\/p>\n<p>for example, p1, p2, p3 have the same distance to p0, then there are P(3,2) = 3 * (3-1) = 6\u00a0Boomerangs<\/p>\n<p>(p1, p0, p2), (p1, p0, p3)<\/p>\n<p>(p2, p0, p1), (p2, p0, p3)<\/p>\n<p>(p3, p0, p1), (p3, p0, p2)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\r\n\/\/ Running time: 210 ms (beats 83.33%)\r\nclass Solution {\r\npublic:\r\n  int numberOfBoomerangs(vector&lt;pair&lt;int, int&gt;&gt;&amp; points) {\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; points.size(); ++i) {\r\n      unordered_map&lt;int, int&gt; dist(points.size());\r\n      for (int j = 0; j &lt; points.size(); ++j) {\r\n        const int dx = points[i].first - points[j].first;\r\n        const int dy = points[i].second - points[j].second;        \r\n        ++dist[dx * dx + dy * dy];\r\n      }\r\n      for (const auto&amp; kv : dist)\r\n        ans += kv.second * (kv.second - 1);      \r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<h2><strong>Solution 2: Sorting<\/strong><\/h2>\n<p>dist = [1, 2, 1, 2, 1, 5]<\/p>\n<p>sorted_dist = [1, 1, 1, 2, 2, 5], 1*3, 2*2, 5*1<\/p>\n<p>ans = 3*(3-1) + 2 * (2 &#8211; 1) * 1 * (1 &#8211; 1) = 8<\/p>\n<p>Time complexity: O(n*nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 150 ms (beats 98.52%)\r\nclass Solution {\r\npublic:\r\n  int numberOfBoomerangs(vector&lt;pair&lt;int, int&gt;&gt;&amp; points) {\r\n    const int n = points.size();\r\n    int ans = 0;\r\n    vector&lt;int&gt; dist(points.size());\r\n    for (int i = 0; i &lt; n; ++i) {   \r\n      for (int j = 0; j &lt; n; ++j) {\r\n        const int dx = points[i].first - points[j].first;\r\n        const int dy = points[i].second - points[j].second;    \r\n        dist[j] = dx * dx + dy * dy;\r\n      }\r\n      \r\n      std::sort(dist.begin(), dist.end());\r\n      \r\n      for (int j = 1; j &lt; n; ++j) {\r\n        int k = 1;\r\n        while (j &lt; n &amp;&amp; dist[j] == dist[j - 1]) { ++j; ++k; }\r\n        ans += k * (k - 1);\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given\u00a0n\u00a0points in the plane that are all pairwise distinct, a &#8220;boomerang&#8221; is a tuple of points\u00a0(i, j, k)\u00a0such that the distance between\u00a0i\u00a0and\u00a0j\u00a0equals the distance&#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,70],"tags":[130,222,82,269],"class_list":["post-2347","post","type-post","status-publish","format-standard","hentry","category-geometry","category-hashtable","tag-distance","tag-easy","tag-hashtable","tag-pairs","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2347","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=2347"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2347\/revisions"}],"predecessor-version":[{"id":2353,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2347\/revisions\/2353"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}