Range Sum Query - Immutable Lab
In this lab, you will be working with the Range Sum Query problem. The goal is to implement the NumArray
class that handles multiple queries of the following type:
- Calculate the sum of the elements of
nums
between indicesleft
andright
inclusive whereleft <= right
.
The NumArray
class should have two main methods:
NumArray(int[] nums)
Initializes the object with the integer arraynums
.int sumRange(int left, int right)
Returns the sum of the elements ofnums
between indicesleft
andright
inclusive (i.e.nums[left] + nums[left + 1] + ... + nums[right]
).
Example:
import NumArray from './index.js'; const numArray = new NumArray([-2, 0, 3, -5, 2, -1]); console.log(numArray.sumRange(0, 2)); // return (-2) + 0 + 3 = 1 console.log(numArray.sumRange(2, 5)); // return 3 + (-5) + 2 + (-1) = -1 console.log(numArray.sumRange(0, 5)); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
Constraints:
1 <= nums.length <= 104
-105 <= nums[i] <= 105
0 <= left <= right < nums.length
- At most
104
calls will be made tosumRange
.
In this lab, you will be given a few challenges to test your implementation of the NumArray
class. Make sure to export the NumArray
class and any additional variables or functions that may be required for testing in the challenges.
The challenges will check your implementation of the NumArray
class and the sumRange
method according to the given constraints. You will be provided with an evaluation script that will test your implementation against various test cases. Make sure to carefully read the instructions and follow the rules outlined above to build a proper lab environment.