Skip to content

Commit 3bfc96a

Browse files
authored
feat: add solutions for lc No.2615 (#5171)
1 parent 35a7040 commit 3bfc96a

4 files changed

Lines changed: 207 additions & 8 deletions

File tree

solution/2600-2699/2615.Sum of Distances/README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ tags:
3232
<strong>输入:</strong>nums = [1,3,1,1,2]
3333
<strong>输出:</strong>[5,0,3,4,0]
3434
<strong>解释:</strong>
35-
i = 0 ,nums[0] == nums[2] 且 nums[0] == nums[3] 。因此,arr[0] = |0 - 2| + |0 - 3| = 5 。
35+
i = 0 ,nums[0] == nums[2] 且 nums[0] == nums[3] 。因此,arr[0] = |0 - 2| + |0 - 3| = 5 。
3636
i = 1 ,arr[1] = 0 因为不存在值等于 3 的其他下标。
3737
i = 2 ,nums[2] == nums[0] 且 nums[2] == nums[3] 。因此,arr[2] = |2 - 0| + |2 - 3| = 3 。
38-
i = 3 ,nums[3] == nums[0] 且 nums[3] == nums[2] 。因此,arr[3] = |3 - 0| + |3 - 2| = 4 。
38+
i = 3 ,nums[3] == nums[0] 且 nums[3] == nums[2] 。因此,arr[3] = |3 - 0| + |3 - 2| = 4 。
3939
i = 4 ,arr[4] = 0 因为不存在值等于 2 的其他下标。
4040
</pre>
4141

@@ -184,6 +184,73 @@ func distance(nums []int) []int64 {
184184
}
185185
```
186186

187+
#### TypeScript
188+
189+
```ts
190+
function distance(nums: number[]): number[] {
191+
const n = nums.length;
192+
const ans = new Array(n).fill(0);
193+
const d = new Map<number, number[]>();
194+
for (let i = 0; i < n; ++i) {
195+
if (!d.has(nums[i])) {
196+
d.set(nums[i], []);
197+
}
198+
d.get(nums[i])!.push(i);
199+
}
200+
for (const idx of d.values()) {
201+
const m = idx.length;
202+
let left = 0;
203+
let right = -1 * m * idx[0];
204+
for (const i of idx) {
205+
right += i;
206+
}
207+
for (let i = 0; i < m; ++i) {
208+
ans[idx[i]] = left + right;
209+
if (i + 1 < m) {
210+
left += (idx[i + 1] - idx[i]) * (i + 1);
211+
right -= (idx[i + 1] - idx[i]) * (m - i - 1);
212+
}
213+
}
214+
}
215+
return ans;
216+
};
217+
```
218+
219+
#### Rust
220+
221+
```rust
222+
use std::collections::HashMap;
223+
224+
impl Solution {
225+
pub fn distance(nums: Vec<i32>) -> Vec<i64> {
226+
let n = nums.len();
227+
let mut ans = vec![0i64; n];
228+
let mut d: HashMap<i32, Vec<usize>> = HashMap::new();
229+
for i in 0..n {
230+
d.entry(nums[i]).or_insert(Vec::new()).push(i);
231+
}
232+
for idx in d.values() {
233+
let m = idx.len();
234+
let mut left = 0i64;
235+
let mut right = 0i64;
236+
for &i in idx {
237+
right += i as i64;
238+
}
239+
right -= m as i64 * idx[0] as i64;
240+
for i in 0..m {
241+
ans[idx[i]] = left + right;
242+
if i + 1 < m {
243+
let diff = (idx[i + 1] - idx[i]) as i64;
244+
left += diff * (i + 1) as i64;
245+
right -= diff * (m - i - 1) as i64;
246+
}
247+
}
248+
}
249+
ans
250+
}
251+
}
252+
```
253+
187254
<!-- tabs:end -->
188255

189256
<!-- solution:end -->

solution/2600-2699/2615.Sum of Distances/README_EN.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> nums = [1,3,1,1,2]
3232
<strong>Output:</strong> [5,0,3,4,0]
33-
<strong>Explanation:</strong>
34-
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
33+
<strong>Explanation:</strong>
34+
When i = 0, nums[0] == nums[2] and nums[0] == nums[3]. Therefore, arr[0] = |0 - 2| + |0 - 3| = 5.
3535
When i = 1, arr[1] = 0 because there is no other index with value 3.
36-
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
37-
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
38-
When i = 4, arr[4] = 0 because there is no other index with value 2.
36+
When i = 2, nums[2] == nums[0] and nums[2] == nums[3]. Therefore, arr[2] = |2 - 0| + |2 - 3| = 3.
37+
When i = 3, nums[3] == nums[0] and nums[3] == nums[2]. Therefore, arr[3] = |3 - 0| + |3 - 2| = 4.
38+
When i = 4, arr[4] = 0 because there is no other index with value 2.
3939

4040
</pre>
4141

@@ -64,7 +64,15 @@ When i = 4, arr[4] = 0 because there is no other index with value 2.
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Hash Map + Prefix Sum
68+
69+
First, use a hash map $d$ to record the list of indices for each element in the array $nums$, that is, $d[x]$ represents the list of all indices in $nums$ where the value is $x$.
70+
71+
For each list of indices $idx$ in the hash map $d$, we can calculate the value of $arr[i]$ for each index $i$ in $idx$. For the first index $idx[0]$, the sum of distances to all indices on the right is $right = \sum_{i=0}^{m-1} - idx[0] \times m$. Then, we iterate through $idx$, and for each iteration, compute $ans[idx[i]] = left + right$, then update $left$ and $right$ as follows: $left = left + (idx[i+1] - idx[i]) \times (i+1)$, and $right = right - (idx[i+1] - idx[i]) \times (m-i-1)$.
72+
73+
After the iteration, we obtain the array $arr$ corresponding to each element in $nums$, which is stored in $ans$.
74+
75+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$.
6876

6977
<!-- tabs:start -->
7078

@@ -178,6 +186,73 @@ func distance(nums []int) []int64 {
178186
}
179187
```
180188

189+
#### TypeScript
190+
191+
```ts
192+
function distance(nums: number[]): number[] {
193+
const n = nums.length;
194+
const ans = new Array(n).fill(0);
195+
const d = new Map<number, number[]>();
196+
for (let i = 0; i < n; ++i) {
197+
if (!d.has(nums[i])) {
198+
d.set(nums[i], []);
199+
}
200+
d.get(nums[i])!.push(i);
201+
}
202+
for (const idx of d.values()) {
203+
const m = idx.length;
204+
let left = 0;
205+
let right = -1 * m * idx[0];
206+
for (const i of idx) {
207+
right += i;
208+
}
209+
for (let i = 0; i < m; ++i) {
210+
ans[idx[i]] = left + right;
211+
if (i + 1 < m) {
212+
left += (idx[i + 1] - idx[i]) * (i + 1);
213+
right -= (idx[i + 1] - idx[i]) * (m - i - 1);
214+
}
215+
}
216+
}
217+
return ans;
218+
};
219+
```
220+
221+
#### Rust
222+
223+
```rust
224+
use std::collections::HashMap;
225+
226+
impl Solution {
227+
pub fn distance(nums: Vec<i32>) -> Vec<i64> {
228+
let n = nums.len();
229+
let mut ans = vec![0i64; n];
230+
let mut d: HashMap<i32, Vec<usize>> = HashMap::new();
231+
for i in 0..n {
232+
d.entry(nums[i]).or_insert(Vec::new()).push(i);
233+
}
234+
for idx in d.values() {
235+
let m = idx.len();
236+
let mut left = 0i64;
237+
let mut right = 0i64;
238+
for &i in idx {
239+
right += i as i64;
240+
}
241+
right -= m as i64 * idx[0] as i64;
242+
for i in 0..m {
243+
ans[idx[i]] = left + right;
244+
if i + 1 < m {
245+
let diff = (idx[i + 1] - idx[i]) as i64;
246+
left += diff * (i + 1) as i64;
247+
right -= diff * (m - i - 1) as i64;
248+
}
249+
}
250+
}
251+
ans
252+
}
253+
}
254+
```
255+
181256
<!-- tabs:end -->
182257

183258
<!-- solution:end -->
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn distance(nums: Vec<i32>) -> Vec<i64> {
5+
let n = nums.len();
6+
let mut ans = vec![0i64; n];
7+
let mut d: HashMap<i32, Vec<usize>> = HashMap::new();
8+
for i in 0..n {
9+
d.entry(nums[i]).or_insert(Vec::new()).push(i);
10+
}
11+
for idx in d.values() {
12+
let m = idx.len();
13+
let mut left = 0i64;
14+
let mut right = 0i64;
15+
for &i in idx {
16+
right += i as i64;
17+
}
18+
right -= m as i64 * idx[0] as i64;
19+
for i in 0..m {
20+
ans[idx[i]] = left + right;
21+
if i + 1 < m {
22+
let diff = (idx[i + 1] - idx[i]) as i64;
23+
left += diff * (i + 1) as i64;
24+
right -= diff * (m - i - 1) as i64;
25+
}
26+
}
27+
}
28+
ans
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function distance(nums: number[]): number[] {
2+
const n = nums.length;
3+
const ans = new Array(n).fill(0);
4+
const d = new Map<number, number[]>();
5+
for (let i = 0; i < n; ++i) {
6+
if (!d.has(nums[i])) {
7+
d.set(nums[i], []);
8+
}
9+
d.get(nums[i])!.push(i);
10+
}
11+
for (const idx of d.values()) {
12+
const m = idx.length;
13+
let left = 0;
14+
let right = -1 * m * idx[0];
15+
for (const i of idx) {
16+
right += i;
17+
}
18+
for (let i = 0; i < m; ++i) {
19+
ans[idx[i]] = left + right;
20+
if (i + 1 < m) {
21+
left += (idx[i + 1] - idx[i]) * (i + 1);
22+
right -= (idx[i + 1] - idx[i]) * (m - i - 1);
23+
}
24+
}
25+
}
26+
return ans;
27+
};

0 commit comments

Comments
 (0)