Skip to content

Commit 8909bfc

Browse files
author
Kohei Asai
authored
303. Range Sum Query - Immutable (#59)
1 parent 3e8811b commit 8909bfc

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import NumArray from "./rangeSumQueryImmutable";
2+
3+
describe("303. Range Sum Query - Immutable", () => {
4+
test("#1", () => {
5+
const numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
6+
7+
expect(numArray.sumRange(0, 2)).toBe(1);
8+
expect(numArray.sumRange(2, 5)).toBe(-1);
9+
expect(numArray.sumRange(0, 5)).toBe(-3);
10+
});
11+
12+
test("#2", () => {
13+
const numArray = new NumArray([
14+
-8261,
15+
2300,
16+
-1429,
17+
6274,
18+
9650,
19+
-3267,
20+
1414,
21+
-8102,
22+
6251,
23+
-5979,
24+
-5291,
25+
-4616,
26+
-4703
27+
]);
28+
29+
expect(numArray.sumRange(0, 8)).toBe(4830);
30+
expect(numArray.sumRange(4, 5)).toBe(6383);
31+
expect(numArray.sumRange(9, 11)).toBe(-15886);
32+
expect(numArray.sumRange(2, 11)).toBe(-5095);
33+
expect(numArray.sumRange(0, 12)).toBe(-15759);
34+
expect(numArray.sumRange(9, 12)).toBe(-20589);
35+
expect(numArray.sumRange(2, 5)).toBe(11228);
36+
expect(numArray.sumRange(0, 9)).toBe(-1149);
37+
expect(numArray.sumRange(5, 9)).toBe(-9683);
38+
expect(numArray.sumRange(0, 1)).toBe(-5961);
39+
expect(numArray.sumRange(12, 12)).toBe(-4703);
40+
expect(numArray.sumRange(6, 7)).toBe(-6688);
41+
expect(numArray.sumRange(5, 8)).toBe(-3704);
42+
expect(numArray.sumRange(1, 1)).toBe(2300);
43+
expect(numArray.sumRange(6, 7)).toBe(-6688);
44+
expect(numArray.sumRange(10, 11)).toBe(-9907);
45+
expect(numArray.sumRange(11, 12)).toBe(-9319);
46+
expect(numArray.sumRange(7, 8)).toBe(-1851);
47+
expect(numArray.sumRange(4, 11)).toBe(-9940);
48+
expect(numArray.sumRange(8, 9)).toBe(272);
49+
expect(numArray.sumRange(4, 11)).toBe(-9940);
50+
expect(numArray.sumRange(2, 9)).toBe(4812);
51+
expect(numArray.sumRange(2, 6)).toBe(12642);
52+
expect(numArray.sumRange(11, 12)).toBe(-9319);
53+
expect(numArray.sumRange(5, 8)).toBe(-3704);
54+
expect(numArray.sumRange(11, 11)).toBe(-4616);
55+
expect(numArray.sumRange(1, 9)).toBe(7112);
56+
expect(numArray.sumRange(12, 12)).toBe(-4703);
57+
expect(numArray.sumRange(10, 10)).toBe(-5291);
58+
expect(numArray.sumRange(4, 7)).toBe(-305);
59+
expect(numArray.sumRange(12, 12)).toBe(-4703);
60+
expect(numArray.sumRange(8, 10)).toBe(-5019);
61+
expect(numArray.sumRange(7, 9)).toBe(-7830);
62+
expect(numArray.sumRange(4, 7)).toBe(-305);
63+
expect(numArray.sumRange(0, 7)).toBe(-1421);
64+
expect(numArray.sumRange(8, 9)).toBe(272);
65+
expect(numArray.sumRange(10, 12)).toBe(-14610);
66+
expect(numArray.sumRange(0, 9)).toBe(-1149);
67+
expect(numArray.sumRange(10, 12)).toBe(-14610);
68+
expect(numArray.sumRange(7, 12)).toBe(-22440);
69+
expect(numArray.sumRange(9, 9)).toBe(-5979);
70+
expect(numArray.sumRange(0, 12)).toBe(-15759);
71+
expect(numArray.sumRange(1, 3)).toBe(7145);
72+
expect(numArray.sumRange(8, 8)).toBe(6251);
73+
expect(numArray.sumRange(7, 10)).toBe(-13121);
74+
});
75+
});

solutions/rangeSumQueryImmutable.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 303. Range Sum Query - Immutable
2+
// https://leetcode.com/problems/range-sum-query-immutable/
3+
class NumArray {
4+
constructor(nums: number[]) {
5+
let total = 0;
6+
7+
// [1, 2, 3, 4] -> [1, 3, 6, 10]
8+
for (let i = 0; i < nums.length; ++i) {
9+
total += nums[i];
10+
11+
this.sums.push(total);
12+
}
13+
}
14+
15+
private sums: number[] = [];
16+
17+
// sumRange(2, 5) == sumRange(0, 5) - sumRange(0, 1)
18+
sumRange(i: number, j: number): number {
19+
return this.sums[j] - (this.sums[i - 1] || 0);
20+
}
21+
}
22+
23+
export default NumArray;

0 commit comments

Comments
 (0)