Skip to content

Commit e19f655

Browse files
authored
Create 2574-left-and-right-sum-differences.js
1 parent f92774b commit e19f655

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const leftRigthDifference = function(nums) {
6+
const {abs} = Math
7+
const n = nums.length
8+
9+
const pre = new Array(n + 2).fill(0)
10+
const post = new Array(n + 2).fill(0)
11+
const res = []
12+
for(let i = 1, cur = 0; i <= n; i++) {
13+
pre[i] = cur
14+
cur += nums[i - 1]
15+
}
16+
17+
for(let i = n, cur = 0; i >= 1; i--) {
18+
post[i] = cur
19+
cur += nums[i - 1]
20+
}
21+
22+
for(let i = 1; i <= n; i++) {
23+
res[i - 1] = abs(pre[i] - post[i])
24+
}
25+
26+
return res
27+
};

0 commit comments

Comments
 (0)