Range Sum Query - Immutable Problem


Description

LeetCode Problem 303.

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]).

Example 1:

1
2
3
4
5
6
7
8
9
10
Input
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
Output
[null, 1, -1, -3]
Explanation
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3

Constraints:

  • 1 <= nums.length <= 10^4
  • -10^5 <= nums[i] <= 10^5
  • 0 <= left <= right < nums.length
  • At most 10^4 calls will be made to sumRange.


Sample C++ Code

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
class NumArray {
public:
    vector<int> dp;
    
    NumArray(vector<int>& nums) {
        if (nums.size() == 0)
            return;
        dp.push_back(nums[0]);
        for (int i = 1; i < nums.size(); i ++) {
            dp.push_back(dp[i-1] + nums[i]);
        }    
    }
    
    int sumRange(int i, int j) {
        if (i == 0)
            return dp[j];
        else
            return (dp[j]-dp[i-1]);
    }
};

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray* obj = new NumArray(nums);
 * int param_1 = obj->sumRange(i,j);
 */




Related Posts

Time Based Key-Value Store Problem

LeetCode 981. Design a time-based key-value data structure that can...

Rle Iterator Problem

LeetCode 900. We can use run-length encoding (i.e., RLE) to...

Online Stock Span Problem

LeetCode 901. Design an algorithm that collects daily price quotes...

Online Election Problem

LeetCode 911. You are given two integer arrays persons and...

Number Of Recent Calls Problem

LeetCode 933. You have a RecentCounter class which counts the...

Maximum Frequency Stack Problem

LeetCode 895. Design a stack-like data structure to push elements...