-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrange-sum-query-immutable.js
More file actions
64 lines (56 loc) · 1.82 KB
/
Copy pathrange-sum-query-immutable.js
File metadata and controls
64 lines (56 loc) · 1.82 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// https://leetcode.com/problems/range-sum-query-immutable/
// Related Topics: Dynamic Programming
// Difficulty: Easy
/*
Initial thoughts:
The naive approach is to look at each and every element every time
and sum them. This would have a Time Complexity of O(n) and a Space Complexity of O(1)
Optimization:
Using a Dynamic Programming approach, we can create an array where array[k] holds the sum
of all the elements from nums[0] to nums[k].
Looking from the sum between i and j (where i <= j) we are going to take array[j] that holds
the sum from nums[0] to nums[j] and subtract array[i] from it (which holds the sum from nums[0] to nums[i])
If we do the preprocessing wisely, it won't take more than O(n) and the lookup time for the consequent sums
is constant.
Please note that this trade wouldn't have made sense if it wasn't specifically stated that the sum of different
ranges will be called repeatedly.
Time complexity: O(n) where n is the number of element in nums
Space complexity: O(n) where n is the number of elements in nums
*/
/**
* @param {number[]} nums
*/
var NumArray = function(nums) {
this.nums = nums;
this.dp = this.preprocess();
};
/**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRange = function(i, j) {
return this.dp[j + 1] - this.dp[i];
};
NumArray.prototype.preprocess = function() {
const dp = Array(this.nums.length + 1).fill(0);
for (i = 0; i < this.nums.length; i++) {
dp[i + 1] = dp[i] + this.nums[i];
}
return dp;
};
/**
* @param {number} i
* @param {number} j
* @return {number}
*/
NumArray.prototype.sumRangeNaive = function(i, j) {
let sum = 0;
for (k = i; k <= j; i++) sum += this.nums[k];
return sum;
};
/**
* Your NumArray object will be instantiated and called as such:
* var obj = new NumArray(nums)
* var param_1 = obj.sumRange(i,j)
*/