Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def sumOddLengthSubarrays(self, arr: List[int]) -> int:
- # Record the sum of all subarrays
- total_sum = 0
-
- # Loop through possible subarray lengths
- for subarray_len in range(1, len(arr) + 1):
- # Skip even-length subarrays
- if subarray_len % 2 == 0:
- continue
-
- # Define start and end indexes for starting subarray
- start_index = 0
- end_index = start_index + subarray_len
-
- # While we haven't reached end of array, sum values in subarray and shift subarray by 1 index
- while end_index <= len(arr):
- total_sum += sum(arr[start_index:end_index])
- start_index += 1
- end_index += 1
-
- # Return total sum
- return total_sum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement