303. Area and Detection-Array Immutable

Topic:
Given an integer array nums, find the sum of the elements in the range from index i to j (i ≤ j), including two points i and j.
Implement the NumArray class:
NumArray(int[] nums) Use the array nums to initialize the object
int sumRange(int i, int j) Return the sum of the elements in the array nums from index i to j (i ≤ j), including i and j (That is, sum(nums[i], nums[i + 1],…, nums[j]))

示例:
输入:
[“NumArray”, “sumRange”, “sumRange”, “sumRange”]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
输出:
[null, 1, -1, -3]
解释:
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))

Tip:
0 <= nums.length <= 104
-105 <= nums[i] <= 105
0 <= i <= j <nums.length
can call the sumRange method up to 104 times

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

Problem-solving ideas:
preSum (prefix sum)
Today’s topic lets us find the sum in an interval [i, j]. The sum of the interval can be done with preSum.
The preSum method can quickly calculate the sum of the elements in the specified interval i-j. Its calculation method is to traverse the array from left to right. When traversing to the i position of the array, preSum represents the sum of the elements on the left of the i position.
Assuming that the length of the array is N, we define a preSum array with a length of N+1, preSum[i] represents the sum of all elements on the left side of the element (not including the i element). Then traverse the array once and accumulate the elements in the range [0, i) to get the preSum array.

The code for preSum is as follows:

Python

N = len(nums)
preSum = range(N + 1)
for i in range(N):
preSum[i + 1] = preSum[i] + nums[i]
print(preSum)
uses the preSum array, which can be used in O( 1) Quickly find the sum of the elements in any interval [i, j] (both ends inclusive) of nums within the time.

sum(i, j) = preSum[j + 1] - preSum[i]

For this question, you can find the preSum of each position of the array in the constructor of the NumArray class; when calculating sumRange(i, j), directly return preSum[j + 1]-preSum[i] to get the interval sum.

The following takes the array [1, 12, -5, -6, 50, 3] as an example to show the calculated preSum.
Insert picture description here
The idea of ​​solving the problem is quoted from: https://leetcode-cn.com/problems/range-sum-query-immutable/solution/presum-qian-zhui-he-xiang-xi-jiang-jie-b-nh23/

Code:

class NumArray:

    def __init__(self, nums):
        n = len(nums)
        self.preNums = [0] * (n+1)
        for i in range(n):
            self.preNums[i+1] = self.preNums[i] + nums[i]

    def sumRange(self, i, j):
        return self.preNums[j+1] - self.preNums[i]

s = NumArray([2,4,1,5,3])
print(s.sumRange(2,4))

Guess you like

Origin blog.csdn.net/annlin2009/article/details/114261307