Skip to content

Commit 5a24bc1

Browse files
committed
Range sum query- Immutable
1 parent 7325f78 commit 5a24bc1

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// 303. Range sum query- Immutable
2+
// Topics: 'Array', 'Design', 'Prefix Sum'
3+
4+
// Given an integer array nums, handle multiple queries of the following type:
5+
6+
// Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.
7+
8+
// Implement the NumArray class:
9+
10+
// NumArray(int[] nums) Initializes the object with the integer array nums.
11+
// 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]).
12+
13+
// Example 1:
14+
15+
// Input
16+
// ["NumArray", "sumRange", "sumRange", "sumRange"]
17+
// [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
18+
// Output
19+
// [null, 1, -1, -3]
20+
21+
// Explanation
22+
// NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
23+
// numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
24+
// numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
25+
// numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
26+
27+
// Constraints:
28+
29+
// 1 <= nums.length <= 104
30+
// -105 <= nums[i] <= 105
31+
// 0 <= left <= right < nums.length
32+
// At most 104 calls will be made to sumRange.
33+
34+
package rangesumqueryimmutable
35+
36+
type NumArray struct {
37+
prefix []int
38+
nums []int
39+
}
40+
41+
func Constructor(nums []int) NumArray {
42+
var prefix []int
43+
var sum int
44+
for _, num := range nums {
45+
sum += num
46+
prefix = append(prefix, sum)
47+
}
48+
return NumArray{prefix: prefix, nums: nums}
49+
}
50+
51+
func (n *NumArray) SumRange(left int, right int) int {
52+
sum := n.prefix[right]
53+
if left == 0 {
54+
return sum
55+
}
56+
sum -= n.prefix[left-1]
57+
return sum
58+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package rangesumqueryimmutable
2+
3+
import "testing"
4+
5+
func TestNumArray(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
nums []int
9+
queries []struct {
10+
left int
11+
right int
12+
want int
13+
}
14+
}{
15+
{
16+
name: "example case",
17+
nums: []int{-2, 0, 3, -5, 2, -1},
18+
queries: []struct {
19+
left int
20+
right int
21+
want int
22+
}{
23+
{0, 2, 1},
24+
{2, 5, -1},
25+
{0, 5, -3},
26+
},
27+
},
28+
{
29+
name: "single element",
30+
nums: []int{5},
31+
queries: []struct {
32+
left int
33+
right int
34+
want int
35+
}{
36+
{0, 0, 5},
37+
},
38+
},
39+
{
40+
name: "all positive numbers",
41+
nums: []int{1, 2, 3, 4, 5},
42+
queries: []struct {
43+
left int
44+
right int
45+
want int
46+
}{
47+
{0, 4, 15},
48+
{1, 3, 9},
49+
{2, 2, 3},
50+
},
51+
},
52+
{
53+
name: "all negative numbers",
54+
nums: []int{-1, -2, -3, -4, -5},
55+
queries: []struct {
56+
left int
57+
right int
58+
want int
59+
}{
60+
{0, 4, -15},
61+
{1, 3, -9},
62+
{3, 4, -9},
63+
},
64+
},
65+
{
66+
name: "zeros",
67+
nums: []int{0, 0, 0, 0},
68+
queries: []struct {
69+
left int
70+
right int
71+
want int
72+
}{
73+
{0, 3, 0},
74+
{1, 2, 0},
75+
},
76+
},
77+
{
78+
name: "left equals right",
79+
nums: []int{10, 20, 30, 40},
80+
queries: []struct {
81+
left int
82+
right int
83+
want int
84+
}{
85+
{0, 0, 10},
86+
{2, 2, 30},
87+
{3, 3, 40},
88+
},
89+
},
90+
{
91+
name: "full range",
92+
nums: []int{1, 2, 3},
93+
queries: []struct {
94+
left int
95+
right int
96+
want int
97+
}{
98+
{0, 2, 6},
99+
},
100+
},
101+
{
102+
name: "mixed positive and negative",
103+
nums: []int{5, -3, 4, -1, 2},
104+
queries: []struct {
105+
left int
106+
right int
107+
want int
108+
}{
109+
{0, 1, 2},
110+
{1, 3, 0},
111+
{2, 4, 5},
112+
},
113+
},
114+
}
115+
116+
for _, tt := range tests {
117+
t.Run(tt.name, func(t *testing.T) {
118+
numArray := Constructor(tt.nums)
119+
for i, q := range tt.queries {
120+
got := numArray.SumRange(q.left, q.right)
121+
if got != q.want {
122+
t.Errorf("query %d: SumRange(%d, %d) = %d, want %d", i, q.left, q.right, got, q.want)
123+
}
124+
}
125+
})
126+
}
127+
}

0 commit comments

Comments
 (0)