← Home

1476. Subrectangle Queries

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
147

Let's make this one less mysterious. For 1476. Subrectangle Queries, the solution in this repository is mainly a data structure design solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are updateSubrectangle, getValue.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 104 ms, faster than 100.00% of C++ online submissions for Subrectangle Queries.
02//Memory Usage: 18.7 MB, less than 66.67% of C++ online submissions for Subrectangle Queries.
03class SubrectangleQueries {
04public:
05    vector<vector<int>> rect;
06    
07    SubrectangleQueries(vector<vector<int>>& rectangle) {
08        this->rect = rectangle;
09    }
10    
11    void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
12        for(int r = row1; r <= row2; r++){
13            for(int c = col1; c <= col2; c++){
14                rect[r][c] = newValue;
15            }
16        }
17    }
18    
19    int getValue(int row, int col) {
20        return rect[row][col];
21    }
22};
23
24/**
25 * Your SubrectangleQueries object will be instantiated and called as such:
26 * SubrectangleQueries* obj = new SubrectangleQueries(rectangle);
27 * obj->updateSubrectangle(row1,col1,row2,col2,newValue);
28 * int param_2 = obj->getValue(row,col);
29 */
30
31//Original and Updates
32//https://leetcode.com/problems/subrectangle-queries/discuss/685500/C%2B%2B-Original-and-Updates
33//Runtime: 100 ms, faster than 100.00% of C++ online submissions for Subrectangle Queries.
34//Memory Usage: 18.5 MB, less than 100.00% of C++ online submissions for Subrectangle Queries.
35class SubrectangleQueries {
36public:
37    vector<vector<int>> rect, subs;
38    
39    SubrectangleQueries(vector<vector<int>>& rectangle) {
40        swap(rectangle, rect);
41    }
42    
43    void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
44        subs.push_back({row1, col1, row2, col2, newValue});
45    }
46    
47    int getValue(int row, int col) {
48        for(int i = subs.size()-1; i >= 0; i--){
49            if(subs[i][0] <= row && row <= subs[i][2] && subs[i][1] <= col && col <= subs[i][3]){
50                //the updated value
51                return subs[i][4];
52            }
53        }
54        return rect[row][col];
55    }
56};
57
58/**
59 * Your SubrectangleQueries object will be instantiated and called as such:
60 * SubrectangleQueries* obj = new SubrectangleQueries(rectangle);
61 * obj->updateSubrectangle(row1,col1,row2,col2,newValue);
62 * int param_2 = obj->getValue(row,col);
63 */

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.