<p>You are given a <strong>0-indexed</strong> array of <code>n</code> integers <code>arr</code>.</p>

<p>The <strong>interval</strong> between two elements in <code>arr</code> is defined as the <strong>absolute difference</strong> between their indices. More formally, the <strong>interval</strong> between <code>arr[i]</code> and <code>arr[j]</code> is <code>|i - j|</code>.</p>

<p>Return <em>an array</em> <code>intervals</code> <em>of length</em> <code>n</code> <em>where</em> <code>intervals[i]</code> <em>is <strong>the sum of intervals</strong> between </em><code>arr[i]</code><em> and each element in </em><code>arr</code><em> with the same value as </em><code>arr[i]</code><em>.</em></p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<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 class="example">Example 2:</strong></p>

<pre>
<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>&nbsp;</p>
<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>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/sum-of-distances/description/" target="_blank"> 2615: Sum of Distances.</a></p>

'''
Brute Force - O(n^2) -> you iterate from 0 to the end, and if the number is the same, just do abs(i - j), add it up, and append to the result

[3 1 1 1 3 1 1 3 1 3] -> 4 + 7 + 9 = 20,

3 + 5 = 8 + 8 = 16 + 4 = 20

[x 3 0]

numbers seen * dist + val

'''
class Solution:
    def getDistances(self, arr: List[int]) -> List[int]:
        # (idx of closest, value, times_seen)
        dp = {}
        res = [0] * len(arr)
        
        for i in range(len(arr) - 1, -1, -1):
            cur_num = arr[i]
            
            if cur_num in dp:
                idx_closest, val, times = dp[cur_num]
                dist = idx_closest - i
                res[i] += val + (times * dist)
                dp[cur_num] = (i, res[i], times + 1)
            else:
                dp[cur_num] = (i, 0, 1)
                
        dp = {}
        
        for i in range(len(arr)):
            cur_num = arr[i]
            
            if cur_num in dp:
                idx_closest, val, times = dp[cur_num]
                dist = i - idx_closest
                to_add = val + (times * dist)
                res[i] += to_add
                dp[cur_num] = (i, to_add, times + 1)
            else:
                dp[cur_num] = (i, 0, 1)
        return res
            
772