3284. Sum of Consecutive Subarrays 🔒
题目描述
We call an array arr
of length n
consecutive if one of the following holds:
arr[i] - arr[i - 1] == 1
for all1 <= i < n
.arr[i] - arr[i - 1] == -1
for all1 <= 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.
Example 1:
Input: nums = [1,2,3]
Output: 20
Explanation:
The consecutive subarrays are: [1]
, [2]
, [3]
, [1, 2]
, [2, 3]
, [1, 2, 3]
.
Sum of their values would be: 1 + 2 + 3 + 3 + 5 + 6 = 20
.
Example 2:
Input: nums = [1,3,5,7]
Output: 16
Explanation:
The consecutive subarrays are: [1]
, [3]
, [5]
, [7]
.
Sum of their values would be: 1 + 3 + 5 + 7 = 16
.
Example 3:
Input: nums = [7,6,1,2]
Output: 32
Explanation:
The consecutive subarrays are: [7]
, [6]
, [1]
, [2]
, [7, 6]
, [1, 2]
.
Sum of their values would be: 7 + 6 + 1 + 2 + 13 + 3 = 32
.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
解法
方法一:递推
我们定义两个变量 $f$ 和 $g$,分别表示以当前元素结尾的递增子数组的长度和以当前元素结尾的递减子数组的长度,用另外两个变量 $s$ 和 $t$ 分别表示以当前元素结尾的递增子数组的和和以当前元素结尾的递减子数组的和。初始时 $f = g = 1$,而 $s = t = \textit{nums}[0]$。
接下来,我们从第二个元素开始遍历数组,对于当前元素 $\textit{nums}[i]$,我们分别考虑以 $\textit{nums}[i]$ 结尾的递增子数组和递减子数组。
如果 $\textit{nums}[i] - \textit{nums}[i - 1] = 1$,那么 $\textit{nums}[i]$ 可以加入到以 $\textit{nums}[i - 1]$ 结尾的递增子数组中,此时我们更新 $f$ 和 $s$,并将 $s$ 加到答案中;
如果 $\textit{nums}[i] - \textit{nums}[i - 1] = -1$,那么 $\textit{nums}[i]$ 可以加入到以 $\textit{nums}[i - 1]$ 结尾的递减子数组中,此时我们更新 $g$ 和 $t$,并将 $t$ 加到答案中。
否则,$\textit{nums}[i]$ 无法加入到以 $\textit{nums}[i - 1]$ 结尾的递增子数组或递减子数组中,我们将 $\textit{nums}[i]$ 加到答案中。
遍历结束后,返回答案即可。
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
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 |
|
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 |
|
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 |
|
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 |
|