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 an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali].
Initially, there is a 0-indexed n x n matrix filled with 0's. For each query, you must apply one of the following changes:
typei == 0, set the values in the row with indexi to vali, overwriting any previous values.typei == 1, set the values in the column with indexi to vali, overwriting any previous values.Return the sum of integers in the matrix after all queries are applied.
Example 1:
Input: n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]] Output: 23 Explanation: The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 23.
Example 2:
Input: n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]] Output: 17 Explanation: The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 17.
Constraints:
1 <= n <= 1041 <= queries.length <= 5 * 104queries[i].length == 30 <= typei <= 10 <= indexi < n0 <= vali <= 105Problem summary: You are given an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali]. Initially, there is a 0-indexed n x n matrix filled with 0's. For each query, you must apply one of the following changes: if typei == 0, set the values in the row with indexi to vali, overwriting any previous values. if typei == 1, set the values in the column with indexi to vali, overwriting any previous values. Return the sum of integers in the matrix after all queries are applied.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
3 [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
3 [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
range-sum-query-2d-mutable)range-addition-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2718: Sum of Matrix After Queries
class Solution {
public long matrixSumQueries(int n, int[][] queries) {
Set<Integer> row = new HashSet<>();
Set<Integer> col = new HashSet<>();
int m = queries.length;
long ans = 0;
for (int k = m - 1; k >= 0; --k) {
var q = queries[k];
int t = q[0], i = q[1], v = q[2];
if (t == 0) {
if (row.add(i)) {
ans += 1L * (n - col.size()) * v;
}
} else {
if (col.add(i)) {
ans += 1L * (n - row.size()) * v;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2718: Sum of Matrix After Queries
func matrixSumQueries(n int, queries [][]int) (ans int64) {
row, col := map[int]bool{}, map[int]bool{}
m := len(queries)
for k := m - 1; k >= 0; k-- {
t, i, v := queries[k][0], queries[k][1], queries[k][2]
if t == 0 {
if !row[i] {
ans += int64(v * (n - len(col)))
row[i] = true
}
} else {
if !col[i] {
ans += int64(v * (n - len(row)))
col[i] = true
}
}
}
return
}
# Accepted solution for LeetCode #2718: Sum of Matrix After Queries
class Solution:
def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:
row = set()
col = set()
ans = 0
for t, i, v in queries[::-1]:
if t == 0:
if i not in row:
ans += v * (n - len(col))
row.add(i)
else:
if i not in col:
ans += v * (n - len(row))
col.add(i)
return ans
// Accepted solution for LeetCode #2718: Sum of Matrix After Queries
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2718: Sum of Matrix After Queries
// class Solution {
// public long matrixSumQueries(int n, int[][] queries) {
// Set<Integer> row = new HashSet<>();
// Set<Integer> col = new HashSet<>();
// int m = queries.length;
// long ans = 0;
// for (int k = m - 1; k >= 0; --k) {
// var q = queries[k];
// int t = q[0], i = q[1], v = q[2];
// if (t == 0) {
// if (row.add(i)) {
// ans += 1L * (n - col.size()) * v;
// }
// } else {
// if (col.add(i)) {
// ans += 1L * (n - row.size()) * v;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2718: Sum of Matrix After Queries
function matrixSumQueries(n: number, queries: number[][]): number {
const row: Set<number> = new Set();
const col: Set<number> = new Set();
let ans = 0;
queries.reverse();
for (const [t, i, v] of queries) {
if (t === 0) {
if (!row.has(i)) {
ans += v * (n - col.size);
row.add(i);
}
} else {
if (!col.has(i)) {
ans += v * (n - row.size);
col.add(i);
}
}
}
return ans;
}
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.