diff --git a/README.md b/README.md
index 24f3733..dfad682 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,7 @@ continually updating π.
* [215. Kth Largest Element in an Array](./src/0215_kth_largest_element_in_an_array/kthleiaa.go) *`sort`*
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go) *`sliding window`*
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go) *`sliding window`*
+* [303. Range Sum Query - Immutable](src/0303_range_sum_query/rsqim.go)
* [349. Intersection of Two Arrays](./src/0349_intersection_of_2_arrays/intersection_of_two_arrays.go) *`set`*
* [350. Intersection of Two Arrays II](./src/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go) *`hash table`*
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go) *`hash table`*
diff --git a/src/0303_range_sum_query/rsqim.go b/src/0303_range_sum_query/rsqim.go
new file mode 100644
index 0000000..333aa16
--- /dev/null
+++ b/src/0303_range_sum_query/rsqim.go
@@ -0,0 +1,30 @@
+/*
+303. Range Sum Query - Immutable
+https://leetcode.com/problems/range-sum-query-immutable/
+
+Given an integer array nums,
+find the sum of the elements between indices i and j (i β€ j), inclusive.
+*/
+// time: 2018εΉ΄12ζ28ζ₯
+
+package rsqim
+
+// NumArray store sum of nums sub-list.
+type NumArray struct {
+ sum []int
+}
+
+// Constructor make NumArray instance.
+func Constructor(nums []int) NumArray {
+ nA := NumArray{sum: make([]int, 1)}
+ for _, num := range nums {
+ nA.sum = append(nA.sum, nA.sum[len(nA.sum)-1]+num)
+ }
+ return nA
+}
+
+// SumRange find the sum of the elements between indices i and j(i <= j), inclusive. +func (na *NumArray) SumRange(i int, j int) int { + j++ + return na.sum[j] - na.sum[i] +} diff --git a/src/0303_range_sum_query/rsqim_test.go b/src/0303_range_sum_query/rsqim_test.go new file mode 100644 index 0000000..9b5dd2b --- /dev/null +++ b/src/0303_range_sum_query/rsqim_test.go @@ -0,0 +1,22 @@ +package rsqim + +import "testing" + +func TestSumRange(t *testing.T) { + nums := []int{-2, 0, 3, -5, 2, -1} + ranges := [][]int{ + {0, 2}, + {2, 5}, + {0, 5}, + } + + expected := []int{1, -1, -3} + + obj := Constructor(nums) + + for index, section := range ranges { + if res := obj.SumRange(section[0], section[1]); res != expected[index] { + t.Errorf("expected %d, got %d", expected[index], res) + } + } +} diff --git a/src/README.md b/src/README.md index a0f90de..4679887 100644 --- a/src/README.md +++ b/src/README.md @@ -40,6 +40,7 @@ |0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*| |0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go)
[Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
+|0303|[303. Range Sum Query - Immutable](0303_range_sum_query/rsqim.go)|Easy||
|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|
|0345|[345. Reverse Vowels of a String](0345_reverse_vowels_of_a_string/reverse_vowels.go)|Easy|*`double index`*|