Problem statement

https://binarysearch.com/problems/Range-Query-on-a-List/

Solution

Equal to Leetcode 0303. Range Sum Query - Immutable.

Complexity

It is just O(n) for time and space for initialization and O(1) time and space for query.

Code

class RangeSum:
    def __init__(self, nums):
        self.arr = [0] + list(accumulate(nums))
        
    def total(self, left, right):
        return self.arr[right] - self.arr[left]