forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRangeSumQuery.kt
38 lines (33 loc) · 1.02 KB
/
RangeSumQuery.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
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given an integer array nums, handle multiple queries of the following type:
* Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.
* [Source](https://leetcode.com/problems/range-sum-query-immutable/)
*/
@UseCommentAsDocumentation
private class NumArray(nums: IntArray) {
val sumArray = IntArray(nums.size) { 0 }
val input = nums
init {
var cumulativeSum = 0
nums.forEachIndexed { index, i ->
cumulativeSum += i
sumArray[index] = cumulativeSum
}
}
fun sumRange(left: Int, right: Int): Int {
val leftSum = sumArray[left]
val rightSum = sumArray[right]
return rightSum - leftSum + input[left]
}
}
fun main() {
run {
val obj = NumArray(intArrayOf(-2, 0, 3, -5, 2, -1))
obj.sumRange(0, 2) shouldBe 1
obj.sumRange(2, 5) shouldBe -1
obj.sumRange(0, 5) shouldBe -3
}
}