3731. Sum of Variable Length Subarrays
My accepted Python solution to LeetCode problem 3731, Sum of Variable Length Subarrays, running in 11ms.
- Difficulty: Easy
- Python
- Runtime 11ms
- Memory 17.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 11ms, memory 17.4MB, accepted 2026-01-01.
class Solution:
def subarraySum(self, nums: List[int]) -> int:
total = 0
for i in range(len(nums)):
start = max(0, i - nums[i])
total += sum(nums[start:i+1])
return total