forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0304-range-sum-query-2d-immutable.kt
More file actions
39 lines (31 loc) · 1018 Bytes
/
0304-range-sum-query-2d-immutable.kt
File metadata and controls
39 lines (31 loc) · 1018 Bytes
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
class NumMatrix(matrix: Array<IntArray>) {
val row = matrix.size
val col = matrix[0].size
val prefixSum = Array(row + 1){ IntArray(col + 1) }
init {
for(i in 0 until row) {
var prefix = 0
for(j in 0 until col) {
println("i: $i j: $j")
prefix += matrix[i][j]
prefixSum[i + 1][j + 1] = prefix + prefixSum[i][j + 1]
}
}
}
fun sumRegion(row1: Int, col1: Int, row2: Int, col2: Int): Int {
val r1 = row1 + 1
val r2 = row2 + 1
val c1 = col1 + 1
val c2 = col2 + 1
val botRight = prefixSum[r2][c2]
val aboveOf = prefixSum[r1 - 1][c2]
val leftOf = prefixSum[r2][c1 - 1]
val topLeft = prefixSum[r1 - 1][c1 - 1]
return botRight - aboveOf - leftOf + topLeft
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* var obj = NumMatrix(matrix)
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
*/