Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b916321

Browse files
authoredDec 12, 2021
Create 2104-sum-of-subarray-ranges.js
1 parent a7ba978 commit b916321

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
 

‎2104-sum-of-subarray-ranges.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const subArrayRanges = function(nums) {
6+
const n = nums.length, { max, min } = Math
7+
let res = 0
8+
9+
for(let i = 0; i < n; i++) {
10+
let [most, least] = [-Infinity, Infinity]
11+
for(let j = i; j < n; j++) {
12+
most = max(most, nums[j])
13+
least = min(least, nums[j])
14+
res += most - least
15+
}
16+
}
17+
return res
18+
};

0 commit comments

Comments
 (0)
Please sign in to comment.