Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes.
You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the following operation:
1 to every element in the submatrix with the top left corner (row1i, col1i) and the bottom right corner (row2i, col2i). That is, add 1 to mat[x][y] for all row1i <= x <= row2i and col1i <= y <= col2i.Return the matrix mat after performing every query.
Example 1:
Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]] Output: [[1,1,0],[1,2,1],[0,1,1]] Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. - In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2). - In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).
Example 2:
Input: n = 2, queries = [[0,0,1,1]] Output: [[1,1],[1,1]] Explanation: The diagram above shows the initial matrix and the matrix after the first query. - In the first query we add 1 to every element in the matrix.
Constraints:
1 <= n <= 5001 <= queries.length <= 1040 <= row1i <= row2i < n0 <= col1i <= col2i < nProblem summary: You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes. You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the following operation: Add 1 to every element in the submatrix with the top left corner (row1i, col1i) and the bottom right corner (row2i, col2i). That is, add 1 to mat[x][y] for all row1i <= x <= row2i and col1i <= y <= col2i. Return the matrix mat after performing every query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
3 [[1,1,2,2],[0,0,1,1]]
2 [[0,0,1,1]]
range-sum-query-2d-mutable)count-positions-on-street-with-required-brightness)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2536: Increment Submatrices by One
class Solution {
public int[][] rangeAddQueries(int n, int[][] queries) {
int[][] mat = new int[n][n];
for (var q : queries) {
int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3];
mat[x1][y1]++;
if (x2 + 1 < n) {
mat[x2 + 1][y1]--;
}
if (y2 + 1 < n) {
mat[x1][y2 + 1]--;
}
if (x2 + 1 < n && y2 + 1 < n) {
mat[x2 + 1][y2 + 1]++;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i > 0) {
mat[i][j] += mat[i - 1][j];
}
if (j > 0) {
mat[i][j] += mat[i][j - 1];
}
if (i > 0 && j > 0) {
mat[i][j] -= mat[i - 1][j - 1];
}
}
}
return mat;
}
}
// Accepted solution for LeetCode #2536: Increment Submatrices by One
func rangeAddQueries(n int, queries [][]int) [][]int {
mat := make([][]int, n)
for i := range mat {
mat[i] = make([]int, n)
}
for _, q := range queries {
x1, y1, x2, y2 := q[0], q[1], q[2], q[3]
mat[x1][y1]++
if x2+1 < n {
mat[x2+1][y1]--
}
if y2+1 < n {
mat[x1][y2+1]--
}
if x2+1 < n && y2+1 < n {
mat[x2+1][y2+1]++
}
}
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i > 0 {
mat[i][j] += mat[i-1][j]
}
if j > 0 {
mat[i][j] += mat[i][j-1]
}
if i > 0 && j > 0 {
mat[i][j] -= mat[i-1][j-1]
}
}
}
return mat
}
# Accepted solution for LeetCode #2536: Increment Submatrices by One
class Solution:
def rangeAddQueries(self, n: int, queries: List[List[int]]) -> List[List[int]]:
mat = [[0] * n for _ in range(n)]
for x1, y1, x2, y2 in queries:
mat[x1][y1] += 1
if x2 + 1 < n:
mat[x2 + 1][y1] -= 1
if y2 + 1 < n:
mat[x1][y2 + 1] -= 1
if x2 + 1 < n and y2 + 1 < n:
mat[x2 + 1][y2 + 1] += 1
for i in range(n):
for j in range(n):
if i:
mat[i][j] += mat[i - 1][j]
if j:
mat[i][j] += mat[i][j - 1]
if i and j:
mat[i][j] -= mat[i - 1][j - 1]
return mat
// Accepted solution for LeetCode #2536: Increment Submatrices by One
impl Solution {
pub fn range_add_queries(n: i32, queries: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = n as usize;
let mut mat = vec![vec![0; n]; n];
for q in queries {
let (x1, y1, x2, y2) = (q[0] as usize, q[1] as usize, q[2] as usize, q[3] as usize);
mat[x1][y1] += 1;
if x2 + 1 < n {
mat[x2 + 1][y1] -= 1;
}
if y2 + 1 < n {
mat[x1][y2 + 1] -= 1;
}
if x2 + 1 < n && y2 + 1 < n {
mat[x2 + 1][y2 + 1] += 1;
}
}
for i in 0..n {
for j in 0..n {
if i > 0 {
mat[i][j] += mat[i - 1][j];
}
if j > 0 {
mat[i][j] += mat[i][j - 1];
}
if i > 0 && j > 0 {
mat[i][j] -= mat[i - 1][j - 1];
}
}
}
mat
}
}
// Accepted solution for LeetCode #2536: Increment Submatrices by One
function rangeAddQueries(n: number, queries: number[][]): number[][] {
const mat: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
for (const [x1, y1, x2, y2] of queries) {
mat[x1][y1] += 1;
if (x2 + 1 < n) mat[x2 + 1][y1] -= 1;
if (y2 + 1 < n) mat[x1][y2 + 1] -= 1;
if (x2 + 1 < n && y2 + 1 < n) mat[x2 + 1][y2 + 1] += 1;
}
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if (i > 0) mat[i][j] += mat[i - 1][j];
if (j > 0) mat[i][j] += mat[i][j - 1];
if (i > 0 && j > 0) mat[i][j] -= mat[i - 1][j - 1];
}
}
return mat;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.