1588. Sum of All Odd Length Subarrays Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays.
A subarray is a contiguous subsequence of the array.
Return the sum of all odd-length subarrays of arr.
Solution 1 - Brute Force
def sumOddLengthSubarrays(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
res = sum(arr)
l = len(arr)
sub_l = 3
while sub_l <= l:
for i in range(l-sub_l+1):
res += sum(arr[i:i+sub_l])
sub_l += 2
return res
Solution 2 - Consider the contribution of A[i]
-
Consider the subarray that contains
A[i]
-
We can take 0,1,2..,i elements on the left, from
A[0]
toA[i]
, we have i + 1 choices. -
we can take 0,1,2..,n-1-i elements on the right, from
A[i]
toA[n-1]
, we have n - i choices. -
In total, there are (i + 1) * (n - i) subarrays, that contains
A[i]
. And there are ((i + 1) * (n - i) + 1) / 2 subarrays with odd length, that containsA[i]
.
def sumOddLengthSubarrays(self, A):
res, n = 0, len(A)
for i, a in enumerate(A):
res += ((i + 1) * (n - i) + 1) / 2 * a
return res