<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hash table Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hash-table/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hash-table/</link>
	<description></description>
	<lastBuildDate>Sun, 26 Dec 2021 13:37:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>hash table Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hash-table/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2121. Intervals Between Identical Elements</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2121-intervals-between-identical-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2121-intervals-between-identical-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Dec 2021 13:34:56 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hash table]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[prefix sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9253</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;array of&#160;n&#160;integers&#160;arr. The&#160;interval&#160;between two elements in&#160;arr&#160;is defined as the&#160;absolute difference&#160;between their indices. More formally, the&#160;interval&#160;between&#160;arr[i]&#160;and&#160;arr[j]&#160;is&#160;&#124;i - j&#124;. Return&#160;an array&#160;intervals&#160;of length&#160;n&#160;where&#160;intervals[i]&#160;is&#160;the sum of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2121-intervals-between-identical-elements/">花花酱 LeetCode 2121. Intervals Between Identical Elements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;array of&nbsp;<code>n</code>&nbsp;integers&nbsp;<code>arr</code>.</p>



<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>



<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>



<p><strong>Note:</strong>&nbsp;<code>|x|</code>&nbsp;is the absolute value of&nbsp;<code>x</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [2,1,3,1,2,3,3]
<strong>Output:</strong> [4,2,7,2,4,4,5]
<strong>Explanation:</strong>
- Index 0: Another 2 is found at index 4. |0 - 4| = 4
- Index 1: Another 1 is found at index 3. |1 - 3| = 2
- Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7
- Index 3: Another 1 is found at index 1. |3 - 1| = 2
- Index 4: Another 2 is found at index 0. |4 - 0| = 4
- Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4
- Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [10,5,10,10]
<strong>Output:</strong> [5,0,3,4]
<strong>Explanation:</strong>
- Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5
- Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.
- Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3
- Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Math / Hashtable + Prefix Sum </strong></h2>



<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>



<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>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;long long&gt; getDistances(vector&lt;int&gt;&amp; arr) {
    const int n = arr.size();
    unordered_map&lt;int, vector&lt;long long&gt;&gt; m;
    vector&lt;int&gt; pos(n);
    for (int i = 0; i &lt; n; ++i) {
      m[arr[i]].push_back(i);
      pos[i] = m[arr[i]].size() - 1;
    }
    for (auto&amp; [k, idx] : m)
      partial_sum(begin(idx), end(idx), begin(idx));      
    vector&lt;long long&gt; ans(n);
    for (int i = 0; i &lt; n; ++i) {
      const auto&amp; sums = m[arr[i]];
      const long long k = pos[i];
      const long long c = sums.size();      
      if (k &gt; 0) ans[i] += k * i - sums[k - 1];
      if (k + 1 &lt; c) ans[i] += (sums[c - 1] - sums[k]) - (c - k - 1) * i;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2121-intervals-between-identical-elements/">花花酱 LeetCode 2121. Intervals Between Identical Elements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-2121-intervals-between-identical-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
