LeetCode 1476: Subrectangle Queries Solution

Master LeetCode problem 1476 (Subrectangle Queries), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1476. Subrectangle Queries

Problem Explanation

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:

Solution Code

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];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1476 (Subrectangle Queries)?

This page provides optimized solutions for LeetCode problem 1476 (Subrectangle Queries) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1476 (Subrectangle Queries)?

The time complexity for LeetCode 1476 (Subrectangle Queries) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1476 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1476 in Java, C++, or Python.

Back to LeetCode Solutions