forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1685.java
More file actions
22 lines (21 loc) · 753 Bytes
/
_1685.java
File metadata and controls
22 lines (21 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.fishercoder.solutions;
public class _1685 {
public static class Solution1 {
public int[] getSumAbsoluteDifferences(int[] nums) {
int len = nums.length;
int[] preSums = new int[len];
for (int i = 1; i < len; i++) {
preSums[i] = preSums[i - 1] + nums[i - 1];
}
int[] postSums = new int[len];
for (int i = len - 2; i >= 0; i--) {
postSums[i] = postSums[i + 1] + nums[i + 1];
}
int[] result = new int[len];
for (int i = 0; i < len; i++) {
result[i] = nums[i] * i - preSums[i] + postSums[i] - nums[i] * (len - i - 1);
}
return result;
}
}
}