Subrectangle Queries

You can implement the SubrectangleQueries class by storing the given rectangle as a class member and then implementing the two methods according to the provided descriptions.

  1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue): This method updates all values within the specified subrectangle with the new value.
  2. getValue(int row, int col): This method returns the value at the given coordinate within the rectangle.

Here’s the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class SubrectangleQueries:

    def __init__(self, rectangle: List[List[int]]):
        self.rectangle = rectangle

    def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
        for row in range(row1, row2 + 1):
            for col in range(col1, col2 + 1):
                self.rectangle[row][col] = newValue

    def getValue(self, row: int, col: int) -> int:
        return self.rectangle[row][col]

In the updateSubrectangle method, we use nested loops to iterate over all the elements in the specified subrectangle and update them to the new value. In the getValue method, we simply return the value at the specified coordinates.