303. Range Sum Query - Immutable

https://leetcode.com/problems/range-sum-query-immutable/

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
Note:
  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.
---

class NumArray {
int[] prefixSum;
public NumArray(int[] nums) {
this.prefixSum = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
this.prefixSum[i] = nums[i] + (i > 0 ? prefixSum[i - 1] : 0);
}
}
public int sumRange(int i, int j) {
return prefixSum[j] - (i > 0 ? prefixSum[i - 1] : 0);
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
class NumArray:
def __init__(self, nums: List[int]):
self.prefix = [0] * len(nums)
for i, v in enumerate(nums):
self.prefix[i] = v + (self.prefix[i - 1] if i > 0 else 0)
def sumRange(self, left: int, right: int) -> int:
return self.prefix[right] - (self.prefix[left - 1] if left > 0 else 0)
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(left,right)