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
Solutions
Solution 1
1 2 3 4 5 6 7 8 9101112131415161718192021
classSubrectangleQueries:def__init__(self,rectangle:List[List[int]]):self.g=rectangleself.ops=[]defupdateSubrectangle(self,row1:int,col1:int,row2:int,col2:int,newValue:int)->None:self.ops.append((row1,col1,row2,col2,newValue))defgetValue(self,row:int,col:int)->int:forr1,c1,r2,c2,vinself.ops[::-1]:ifr1<=row<=r2andc1<=col<=c2:returnvreturnself.g[row][col]# Your SubrectangleQueries object will be instantiated and called as such:# obj = SubrectangleQueries(rectangle)# obj.updateSubrectangle(row1,col1,row2,col2,newValue)# param_2 = obj.getValue(row,col)
classSubrectangleQueries{privateint[][]g;privateLinkedList<int[]>ops=newLinkedList<>();publicSubrectangleQueries(int[][]rectangle){g=rectangle;}publicvoidupdateSubrectangle(introw1,intcol1,introw2,intcol2,intnewValue){ops.addFirst(newint[]{row1,col1,row2,col2,newValue});}publicintgetValue(introw,intcol){for(varop:ops){if(op[0]<=row&&row<=op[2]&&op[1]<=col&&col<=op[3]){returnop[4];}}returng[row][col];}}/** * 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); */
classSubrectangleQueries{public:vector<vector<int>>g;vector<vector<int>>ops;SubrectangleQueries(vector<vector<int>>&rectangle){g=rectangle;}voidupdateSubrectangle(introw1,intcol1,introw2,intcol2,intnewValue){ops.push_back({row1,col1,row2,col2,newValue});}intgetValue(introw,intcol){for(inti=ops.size()-1;~i;--i){autoop=ops[i];if(op[0]<=row&&row<=op[2]&&op[1]<=col&&col<=op[3]){returnop[4];}}returng[row][col];}};/** * 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); */
typeSubrectangleQueriesstruct{g[][]intops[][]int}funcConstructor(rectangle[][]int)SubrectangleQueries{returnSubrectangleQueries{rectangle,[][]int{}}}func(this*SubrectangleQueries)UpdateSubrectangle(row1int,col1int,row2int,col2int,newValueint){this.ops=append(this.ops,[]int{row1,col1,row2,col2,newValue})}func(this*SubrectangleQueries)GetValue(rowint,colint)int{fori:=len(this.ops)-1;i>=0;i--{op:=this.ops[i]ifop[0]<=row&&row<=op[2]&&op[1]<=col&&col<=op[3]{returnop[4]}}returnthis.g[row][col]}/** * Your SubrectangleQueries object will be instantiated and called as such: * obj := Constructor(rectangle); * obj.UpdateSubrectangle(row1,col1,row2,col2,newValue); * param_2 := obj.GetValue(row,col); */
classSubrectangleQueries{g:number[][];ops:number[][];constructor(rectangle:number[][]){this.g=rectangle;this.ops=[];}updateSubrectangle(row1:number,col1:number,row2:number,col2:number,newValue:number,):void{this.ops.push([row1,col1,row2,col2,newValue]);}getValue(row:number,col:number):number{for(leti=this.ops.length-1;~i;--i){const[r1,c1,r2,c2,v]=this.ops[i];if(r1<=row&&row<=r2&&c1<=col&&col<=c2){returnv;}}returnthis.g[row][col];}}/** * Your SubrectangleQueries object will be instantiated and called as such: * var obj = new SubrectangleQueries(rectangle) * obj.updateSubrectangle(row1,col1,row2,col2,newValue) * var param_2 = obj.getValue(row,col) */