LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 11ms, memory 17.4MB, accepted 2026-01-01.

python
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

Source