{"id":9253,"date":"2021-12-26T05:34:56","date_gmt":"2021-12-26T13:34:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9253"},"modified":"2021-12-26T05:37:10","modified_gmt":"2021-12-26T13:37:10","slug":"leetcode-2121-intervals-between-identical-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2121-intervals-between-identical-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2121. Intervals Between Identical Elements"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array of&nbsp;<code>n<\/code>&nbsp;integers&nbsp;<code>arr<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>interval<\/strong>&nbsp;between two elements in&nbsp;<code>arr<\/code>&nbsp;is defined as the&nbsp;<strong>absolute difference<\/strong>&nbsp;between their indices. More formally, the&nbsp;<strong>interval<\/strong>&nbsp;between&nbsp;<code>arr[i]<\/code>&nbsp;and&nbsp;<code>arr[j]<\/code>&nbsp;is&nbsp;<code>|i - j|<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an array<\/em>&nbsp;<code>intervals<\/code>&nbsp;<em>of length<\/em>&nbsp;<code>n<\/code>&nbsp;<em>where<\/em>&nbsp;<code>intervals[i]<\/code>&nbsp;<em>is&nbsp;<strong>the sum of intervals<\/strong>&nbsp;between&nbsp;<\/em><code>arr[i]<\/code><em>&nbsp;and each element in&nbsp;<\/em><code>arr<\/code><em>&nbsp;with the same value as&nbsp;<\/em><code>arr[i]<\/code><em>.<\/em><\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;<code>|x|<\/code>&nbsp;is the absolute value of&nbsp;<code>x<\/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> arr = [2,1,3,1,2,3,3]\n<strong>Output:<\/strong> [4,2,7,2,4,4,5]\n<strong>Explanation:<\/strong>\n- Index 0: Another 2 is found at index 4. |0 - 4| = 4\n- Index 1: Another 1 is found at index 3. |1 - 3| = 2\n- Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7\n- Index 3: Another 1 is found at index 1. |3 - 1| = 2\n- Index 4: Another 2 is found at index 0. |4 - 0| = 4\n- Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4\n- Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5\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> arr = [10,5,10,10]\n<strong>Output:<\/strong> [5,0,3,4]\n<strong>Explanation:<\/strong>\n- Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5\n- Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.\n- Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3\n- Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == arr.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math \/ Hashtable + Prefix Sum <\/strong><\/h2>\n\n\n\n<p>For each arr[i], suppose it occurs in the array of total c times, among which k of them are in front of it and c &#8211; k &#8211; 1 of them are after it. Then the total sum intervals:<br>(i &#8211; j<sub>1<\/sub>) + (i &#8211; j<sub>2<\/sub>) + &#8230; + (i &#8211; j<sub>k<\/sub>) + (j<sub>k+1<\/sub>-i) + (j<sub>k+2<\/sub>-i) + &#8230; + (j<sub>c<\/sub>-i)<br>&lt;=&gt; k * i &#8211; sum(j<sub>1<\/sub>~j<sub>k<\/sub>)  + sum(j<sub>k+1<\/sub>~j<sub>c<\/sub>) &#8211; (c &#8211; k &#8211; 1) * i<\/p>\n\n\n\n<p>Use a hashtable to store the indies of each unique number in the array and compute the prefix sum for fast range sum query.<\/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  vector<long long> getDistances(vector<int>& arr) {\n    const int n = arr.size();\n    unordered_map<int, vector<long long>> m;\n    vector<int> pos(n);\n    for (int i = 0; i < n; ++i) {\n      m[arr[i]].push_back(i);\n      pos[i] = m[arr[i]].size() - 1;\n    }\n    for (auto&#038; [k, idx] : m)\n      partial_sum(begin(idx), end(idx), begin(idx));      \n    vector<long long> ans(n);\n    for (int i = 0; i < n; ++i) {\n      const auto&#038; sums = m[arr[i]];\n      const long long k = pos[i];\n      const long long c = sums.size();      \n      if (k > 0) ans[i] += k * i - sums[k - 1];\n      if (k + 1 < c) ans[i] += (sums[c - 1] - sums[k]) - (c - k - 1) * i;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array of&nbsp;n&nbsp;integers&nbsp;arr. The&nbsp;interval&nbsp;between two elements in&nbsp;arr&nbsp;is defined as the&nbsp;absolute difference&nbsp;between their indices. More formally, the&nbsp;interval&nbsp;between&nbsp;arr[i]&nbsp;and&nbsp;arr[j]&nbsp;is&nbsp;|i &#8211; j|. Return&nbsp;an array&nbsp;intervals&nbsp;of length&nbsp;n&nbsp;where&nbsp;intervals[i]&nbsp;is&nbsp;the sum of&#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":[752,31,200],"class_list":["post-9253","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hash-table","tag-math","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9253","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=9253"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9253\/revisions"}],"predecessor-version":[{"id":9255,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9253\/revisions\/9255"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}