LeetCode 1476: Subrectangle Queries

ArrayDesignMatrix

Problem Description

Explanation:

To solve this problem, we can maintain the rectangle as a 2D array and update the values in the specified subrectangle directly. We can then retrieve the value of a specific cell by accessing its corresponding indices in the 2D array.

For updating a subrectangle, we iterate over the specified range and update each cell with the new value. For getting the value of a cell, we simply return the value at the specified indices in the 2D array. Solution:

Solutions

class SubrectangleQueries {
    int[][] rectangle;

    public SubrectangleQueries(int[][] rectangle) {
        this.rectangle = rectangle;
    }

    public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        for (int i = row1; i <= row2; i++) {
            for (int j = col1; j <= col2; j++) {
                rectangle[i][j] = newValue;
            }
        }
    }

    public int getValue(int row, int col) {
        return rectangle[row][col];
    }
}

Loading editor...