-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangeSumQueryImmutable.ts
More file actions
30 lines (27 loc) · 936 Bytes
/
rangeSumQueryImmutable.ts
File metadata and controls
30 lines (27 loc) · 936 Bytes
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
// First approach using O(1) Time-Complexity on nums storing, and O(n) on index calculation
class NumArray {
nums: number[];
constructor(nums: number[]) {
this.nums = nums;
}
sumRange(left: number, right: number): number {
let sum: number = 0;
for(let i = left; i <= right; i++) {
sum += this.nums[i];
}
return sum;
}
}
// Second approach using O(n) Time-Complexity on result storing for prefix sum, and O(1) on index calculation using prefix sum.
class NumArray {
result: number[] = [];
constructor(nums: number[]) {
this.result[0] = nums[0];
for(let i = 1; i < nums.length; i++) {
this.result[i] = this.result[i - 1] + nums[i];
}
}
sumRange(left: number, right: number): number {
return left > 0 ? this.result[right] - this.result[left - 1] : this.result[right];
}
}