-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path304. Range Sum Query 2D - Immutable.rb
More file actions
44 lines (38 loc) · 1.06 KB
/
304. Range Sum Query 2D - Immutable.rb
File metadata and controls
44 lines (38 loc) · 1.06 KB
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
class NumMatrix
=begin
:type matrix: Integer[][]
=end
def initialize(matrix)
@matrix = Array.new(matrix.length) { |i| i = Array.new(matrix[0].length) { |i| } }
for i in 0..matrix.length-1 do
@matrix[i][0] = matrix[i][0]
for j in 1..matrix[0].length-1 do
@matrix[i][j] = @matrix[i][j-1] + matrix[i][j]
end
end
for i in 0..@matrix.length-1 do
@matrix[i].unshift(0)
end
end
=begin
:type row1: Integer
:type col1: Integer
:type row2: Integer
:type col2: Integer
:rtype: Integer
=end
def sum_region(row1, col1, row2, col2)
if [row2, col2] == [0,0] then return @matrix[row1][col1+1] end
col1 += 1
col2 += 1
base = col1 - 1
sum = 0
for i in row1..row2 do
sum += (@matrix[i][col2] - @matrix[i][base])
end
sum
end
end
# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix.new(matrix)
# param_1 = obj.sum_region(row1, col1, row2, col2)