Subrectangle Queries - Problem

Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods:

1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)

Updates all values with newValue in the subrectangle whose upper left coordinate is (row1,col1) and bottom right coordinate is (row2,col2).

2. getValue(int row, int col)

Returns the current value of the coordinate (row,col) from the rectangle.

Input & Output

Example 1 — Basic Operations
$ Input: [["SubrectangleQueries",[[1,2,1],[4,3,4],[3,2,1]]],["getValue",[0,2]],["updateSubrectangle",[0,0,2,2,5]],["getValue",[0,2]],["getValue",[2,1]]]
Output: [null,1,null,5,5]
💡 Note: Initialize with matrix [[1,2,1],[4,3,4],[3,2,1]], getValue(0,2) returns 1, update subrectangle to 5, getValue(0,2) now returns 5, getValue(2,1) returns 5
Example 2 — Multiple Updates
$ Input: [["SubrectangleQueries",[[1,1,1],[2,2,2],[3,3,3]]],["updateSubrectangle",[0,0,1,1,10]],["getValue",[0,0]],["updateSubrectangle",[1,1,2,2,20]],["getValue",[1,1]]]
Output: [null,null,10,null,20]
💡 Note: First update sets top-left 2x2 to 10, second update overlaps and sets bottom-right 2x2 to 20
Example 3 — Edge Case Single Cell
$ Input: [["SubrectangleQueries",[[5]]],["getValue",[0,0]],["updateSubrectangle",[0,0,0,0,3]],["getValue",[0,0]]]
Output: [null,5,null,3]
💡 Note: Single cell matrix, getValue returns 5, update single cell to 3, getValue returns 3

Constraints

  • 1 ≤ rows, cols ≤ 100
  • 0 ≤ row1 ≤ row2 < rows
  • 0 ≤ col1 ≤ col2 < cols
  • 1 ≤ newValue, rectangle[i][j] ≤ 109
  • At most 500 calls to updateSubrectangle and getValue

Visualization

Tap to expand
Subrectangle Queries - Update History Tracking INPUT Initial 3x3 Matrix: 1 2 1 4 3 4 3 2 1 0 1 2 0 1 2 Operations: 1. getValue(0,2) 2. updateSubrectangle (0,0,2,2,5) 3. getValue(0,2) 4. getValue(2,1) Update covers (0,0) to (2,2) Entire matrix updated to value: 5 ALGORITHM STEPS 1 Store Matrix Keep original values 2 Track Updates Store as history list 3 getValue Logic Check history backward 4 Return Value Latest or original Update History Stack: [0,0,2,2,5] - Latest (empty before update) getValue(r,c): scan history from newest to oldest return first match or original scan FINAL RESULT Matrix after update: 5 5 5 5 5 5 5 5 5 Query Results: getValue(0,2) before: 1 getValue(0,2) after: 5 getValue(2,1) after: 5 Output: [null,1,null,5,5] OK - All queries resolved! Key Insight: Instead of modifying the matrix on each update (O(m*n)), store updates as history entries. For getValue: scan history backward to find the most recent update covering (row,col). Update: O(1) | getValue: O(history size) - Efficient when updates are frequent! TutorialsPoint - Subrectangle Queries | Update History Tracking Approach
Asked in
Google 12 Amazon 8 Microsoft 6
25.3K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen