-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path303.range-sum-query-immutable.kt
55 lines (50 loc) · 1.12 KB
/
303.range-sum-query-immutable.kt
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
/*
* @lc app=leetcode id=303 lang=kotlin
*
* [303] Range Sum Query - Immutable
*
* https://leetcode.com/problems/range-sum-query-immutable/description/
*
* algorithms
* Easy (39.27%)
* Likes: 528
* Dislikes: 832
* Total Accepted: 150.6K
* Total Submissions: 382.7K
* Testcase Example: '["NumArray","sumRange","sumRange","sumRange"]\n[[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]'
*
* Given an integer array nums, find the sum of the elements between indices i
* and j (i ≤ j), inclusive.
*
* Example:
*
* Given nums = [-2, 0, 3, -5, 2, -1]
*
* sumRange(0, 2) -> 1
* sumRange(2, 5) -> -1
* sumRange(0, 5) -> -3
*
*
*
* Note:
*
* You may assume that the array does not change.
* There are many calls to sumRange function.
*
*
*/
class NumArray(nums: IntArray) {
var dp = IntArray(nums.size+1)
init {
for (i in 1 .. nums.size)
dp[i] = dp[i-1]+nums[i-1]
}
fun sumRange(i: Int, j: Int): Int {
return dp[j+1]-dp[i]
}
}
/**
* Your NumArray object will be instantiated and called as such:
* var obj = NumArray(nums)
* var param_1 = obj.sumRange(i,j)
*/