-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3284-sum-of-consecutive-subarrays.js
More file actions
50 lines (46 loc) · 1.57 KB
/
3284-sum-of-consecutive-subarrays.js
File metadata and controls
50 lines (46 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 3284. Sum of Consecutive Subarrays
* https://leetcode.com/problems/sum-of-consecutive-subarrays/
* Difficulty: Medium
*
* We call an array arr of length n consecutive if one of the following holds:
* - arr[i] - arr[i - 1] == 1 for all 1 <= i < n.
* - arr[i] - arr[i - 1] == -1 for all 1 <= i < n.
*
* The value of an array is the sum of its elements.
*
* For example, [3, 4, 5] is a consecutive array of value 12 and [9, 8] is another of
* value 17. While [3, 4, 3] and [8, 6] are not consecutive.
*
* Given an array of integers nums, return the sum of the values of all consecutive subarrays.
*
* Since the answer may be very large, return it modulo 109 + 7.
*
* Note that an array of length 1 is also considered consecutive.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var getSum = function(nums) {
const MOD = 1e9 + 7;
let result = nums[0];
let currentContribution = nums[0];
let leftBoundary = 0;
let previousDifference = 0;
for (let rightIndex = 1; rightIndex < nums.length; rightIndex++) {
const currentDifference = nums[rightIndex] - nums[rightIndex - 1];
if (Math.abs(currentDifference) !== 1) {
previousDifference = 0;
leftBoundary = rightIndex;
currentContribution = 0;
} else if (previousDifference !== currentDifference) {
currentContribution = nums[rightIndex - 1];
leftBoundary = rightIndex - 1;
previousDifference = currentDifference;
}
currentContribution += nums[rightIndex] * (rightIndex - leftBoundary + 1);
result = (result + currentContribution) % MOD;
}
return result;
};