Leetcode 1693 Solution

This article provides solution to leetcode question 1693 (sum-of-all-odd-length-subarrays)

https://leetcode.com/problems/sum-of-all-odd-length-subarrays

Solution

class Solution:
    def sumOddLengthSubarrays(self, arr: List[int]) -> int:
        arr = [0] + arr

        n = len(arr)
        sums = []

        s = 0
        for v in arr:
            s += v
            sums.append(s)

        ans = 0
        for i in range(len(arr)):
            j = i

            while j >= 1:
                ans += sums[i] - sums[j - 1]
                j -= 2

        return ans