Class Solution

java.lang.Object
g2601_2700.s2615_sum_of_distances.Solution

public class Solution extends Object
2615 - Sum of Distances.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. There exists an array <code>arr</code> of length <code>nums.length</code>, where <code>arr[i]</code> is the sum of <code>|i - j|</code> over all <code>j</code> such that <code>nums[j] == nums[i]</code> and <code>j != i</code>. If there is no such <code>j</code>, set <code>arr[i]</code> to be <code>0</code>.</p> <p>Return <em>the array</em> <code>arr</code><em>.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,3,1,1,2]</p> <p><strong>Output:</strong> [5,0,3,4,0]</p> <p><strong>Explanation:</strong></p> <p>When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.</p> <p>When i = 1, arr[1] = 0 because there is no other index with value 3.</p> <p>When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.</p> <p>When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.</p> <p>When i = 4, arr[4] = 0 because there is no other index with value 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [0,5,3]</p> <p><strong>Output:</strong> [0,0,0]</p> <p><strong>Explanation:</strong> Since each element in nums is distinct, arr[i] = 0 for all i.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>0 <= nums[i] <= 10<sup>9</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • distance

      public long[] distance(int[] nums)