Skip to content

Commit 7563cb1

Browse files
committed
feat: add solutions to lc problem: No.1685. Sum of Absolute Differences
in a Sorted Array
1 parent f6488e3 commit 7563cb1

File tree

6 files changed

+182
-4
lines changed

6 files changed

+182
-4
lines changed

solution/1600-1699/1685.Sum of Absolute Differences in a Sorted Array/README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,90 @@ result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5。
4141
<li><code>1 <= nums[i] <= nums[i + 1] <= 10<sup>4</sup></code></li>
4242
</ul>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
4847

48+
前缀和实现。
49+
4950
<!-- tabs:start -->
5051

5152
### **Python3**
5253

5354
<!-- 这里可写当前语言的特殊实现逻辑 -->
5455

5556
```python
56-
57+
class Solution:
58+
def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:
59+
n = len(nums)
60+
presum = [0] * (n + 1)
61+
for i in range(n):
62+
presum[i + 1] = presum[i] + nums[i]
63+
res = []
64+
for i, num in enumerate(nums):
65+
t = num * i - presum[i] + presum[n] - presum[i + 1] - num * (n - i - 1)
66+
res.append(t)
67+
return res
5768
```
5869

5970
### **Java**
6071

6172
<!-- 这里可写当前语言的特殊实现逻辑 -->
6273

6374
```java
75+
class Solution {
76+
public int[] getSumAbsoluteDifferences(int[] nums) {
77+
int n = nums.length;
78+
int[] presum = new int[n + 1];
79+
for (int i = 0; i < n; ++i) {
80+
presum[i + 1] = presum[i] + nums[i];
81+
}
82+
int[] res = new int[n];
83+
for (int i = 0; i < n; ++i) {
84+
res[i] = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1);
85+
}
86+
return res;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
97+
int n = nums.size();
98+
vector<int> presum(n + 1);
99+
for (int i = 0; i < n; ++i) {
100+
presum[i + 1] = presum[i] + nums[i];
101+
}
102+
vector<int> res;
103+
for (int i = 0; i < n; ++i) {
104+
int t = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1);
105+
res.push_back(t);
106+
}
107+
return res;
108+
}
109+
};
110+
```
64111
112+
### **Go**
113+
114+
```go
115+
func getSumAbsoluteDifferences(nums []int) []int {
116+
n := len(nums)
117+
presum := make([]int, n+1)
118+
for i := 0; i < n; i++ {
119+
presum[i+1] = presum[i] + nums[i]
120+
}
121+
var res []int
122+
for i := 0; i < n; i++ {
123+
t := nums[i]*i - presum[i] + presum[n] - presum[i+1] - nums[i]*(n-i-1)
124+
res = append(res, t)
125+
}
126+
return res
127+
}
65128
```
66129

67130
### **...**

solution/1600-1699/1685.Sum of Absolute Differences in a Sorted Array/README_EN.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,82 @@ result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
3737
<li><code>1 &lt;= nums[i] &lt;= nums[i + 1] &lt;= 10<sup>4</sup></code></li>
3838
</ul>
3939

40-
4140
## Solutions
4241

4342
<!-- tabs:start -->
4443

4544
### **Python3**
4645

4746
```python
48-
47+
class Solution:
48+
def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:
49+
n = len(nums)
50+
presum = [0] * (n + 1)
51+
for i in range(n):
52+
presum[i + 1] = presum[i] + nums[i]
53+
res = []
54+
for i, num in enumerate(nums):
55+
t = num * i - presum[i] + presum[n] - presum[i + 1] - num * (n - i - 1)
56+
res.append(t)
57+
return res
4958
```
5059

5160
### **Java**
5261

5362
```java
63+
class Solution {
64+
public int[] getSumAbsoluteDifferences(int[] nums) {
65+
int n = nums.length;
66+
int[] presum = new int[n + 1];
67+
for (int i = 0; i < n; ++i) {
68+
presum[i + 1] = presum[i] + nums[i];
69+
}
70+
int[] res = new int[n];
71+
for (int i = 0; i < n; ++i) {
72+
res[i] = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1);
73+
}
74+
return res;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
85+
int n = nums.size();
86+
vector<int> presum(n + 1);
87+
for (int i = 0; i < n; ++i) {
88+
presum[i + 1] = presum[i] + nums[i];
89+
}
90+
vector<int> res;
91+
for (int i = 0; i < n; ++i) {
92+
int t = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1);
93+
res.push_back(t);
94+
}
95+
return res;
96+
}
97+
};
98+
```
5499
100+
### **Go**
101+
102+
```go
103+
func getSumAbsoluteDifferences(nums []int) []int {
104+
n := len(nums)
105+
presum := make([]int, n+1)
106+
for i := 0; i < n; i++ {
107+
presum[i+1] = presum[i] + nums[i]
108+
}
109+
var res []int
110+
for i := 0; i < n; i++ {
111+
t := nums[i]*i - presum[i] + presum[n] - presum[i+1] - nums[i]*(n-i-1)
112+
res = append(res, t)
113+
}
114+
return res
115+
}
55116
```
56117

57118
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<int> getSumAbsoluteDifferences(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> presum(n + 1);
6+
for (int i = 0; i < n; ++i) {
7+
presum[i + 1] = presum[i] + nums[i];
8+
}
9+
vector<int> res;
10+
for (int i = 0; i < n; ++i) {
11+
int t = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1);
12+
res.push_back(t);
13+
}
14+
return res;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func getSumAbsoluteDifferences(nums []int) []int {
2+
n := len(nums)
3+
presum := make([]int, n+1)
4+
for i := 0; i < n; i++ {
5+
presum[i+1] = presum[i] + nums[i]
6+
}
7+
var res []int
8+
for i := 0; i < n; i++ {
9+
t := nums[i]*i - presum[i] + presum[n] - presum[i+1] - nums[i]*(n-i-1)
10+
res = append(res, t)
11+
}
12+
return res
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int[] getSumAbsoluteDifferences(int[] nums) {
3+
int n = nums.length;
4+
int[] presum = new int[n + 1];
5+
for (int i = 0; i < n; ++i) {
6+
presum[i + 1] = presum[i] + nums[i];
7+
}
8+
int[] res = new int[n];
9+
for (int i = 0; i < n; ++i) {
10+
res[i] = nums[i] * i - presum[i] + presum[n] - presum[i + 1] - nums[i] * (n - i - 1);
11+
}
12+
return res;
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]:
3+
n = len(nums)
4+
presum = [0] * (n + 1)
5+
for i in range(n):
6+
presum[i + 1] = presum[i] + nums[i]
7+
res = []
8+
for i, num in enumerate(nums):
9+
t = num * i - presum[i] + presum[n] - presum[i + 1] - num * (n - i - 1)
10+
res.append(t)
11+
return res

0 commit comments

Comments
 (0)