-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathSubrectangle Queries.java
More file actions
32 lines (28 loc) · 1.02 KB
/
Subrectangle Queries.java
File metadata and controls
32 lines (28 loc) · 1.02 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
class SubrectangleQueries {
List<int[]> modifications;
int[][] rectangle;
public SubrectangleQueries(int[][] rectangle) {
this.rectangle = rectangle;
modifications = new ArrayList<>();
}
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
modifications.add(new int[]{row1, col1, row2, col2, newValue});
}
public int getValue(int row, int col) {
for (int i = modifications.size() - 1; i >= 0; i--) {
if (inRange(row, col, modifications.get(i))) {
return modifications.get(i)[4];
}
}
return rectangle[row][col];
}
private boolean inRange(int row, int col, int[] modification) {
return row >= modification[0] && row <= modification[2] && col >= modification[1] && col <= modification[3];
}
}
/**
* Your SubrectangleQueries object will be instantiated and called as such:
* SubrectangleQueries obj = new SubrectangleQueries(rectangle);
* obj.updateSubrectangle(row1,col1,row2,col2,newValue);
* int param_2 = obj.getValue(row,col);
*/