-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRange_Sum_Query_Immutable.py
28 lines (21 loc) · 1.09 KB
/
Range_Sum_Query_Immutable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''Given an integer array nums, handle multiple queries of the following type:
Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.
Implement the NumArray class:
NumArray(int[] nums) Initializes the object with the integer array nums.
int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).'''
class NumArray:
sum_nums: List[int] = List[int]
def __init__(self, nums: List[int]):
self.sum_nums = [0] * len(nums)
for i in range(0, len(nums)):
if i == 0:
self.sum_nums[i] = nums[i]
else:
self.sum_nums[i] = self.sum_nums[i-1] + nums[i]
def sumRange(self, left: int, right: int) -> int:
if left == 0:
return self.sum_nums[right]
return self.sum_nums[right] - self.sum_nums[left - 1]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(left,right)