There will be at most 500 operations considering both methods: updateSubrectangle and getValue.
1 <= rows, cols <= 100
rows == rectangle.length
cols == rectangle[i].length
0 <= row1 <= row2 < rows
0 <= col1 <= col2 < cols
1 <= newValue, rectangle[i][j] <= 10^9
0 <= row < rows
0 <= col < cols
Solution:
class SubrectangleQueries {
int[][] original;
List<List<Integer>> updates;
public SubrectangleQueries(int[][] rectangle) {
original = rectangle;
updates = new ArrayList();
}
public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
updates.add(Arrays.asList(row1, col1, row2, col2, newValue));
}
public int getValue(int row, int col) {
for (int i = updates.size() - 1; i >= 0; i --) {
List<Integer> update = updates.get(i);
if (included(update, row, col)) {
return update.get(4);
}
}
return original[row][col];
}
private boolean included(List<Integer> update, int row, int col) {
int row1 = update.get(0);
int col1 = update.get(1);
int row2 = update.get(2);
int col2 = update.get(3);
if (row1 > row) return false;
if (col1 > col) return false;
if (row2 < row) return false;
if (col2 < col) return false;
return true;
}
}
/**
* 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);
*/