Range Sum Query - Immutable

Problem Summary

Why This Works

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class NumArray:
    def __init__(self, nums: List[int]):
        total = 0
        self.prefix = []
        for num in nums:
            total += num
            self.prefix.append(total)

    def sumRange(self, left: int, right: int) -> int:
        leftValue = self.prefix[left - 1] if left > 0 else 0
        rightValue = self.prefix[right]
        return rightValue - leftValue

Common Mistakes / Things I Got Stuck On