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 0-indexed integer array arr, and an m x n integer matrix mat. arr and mat both contain all the integers in the range [1, m * n].
Go through each index i in arr starting from index 0 and paint the cell in mat containing the integer arr[i].
Return the smallest index i at which either a row or a column will be completely painted in mat.
Example 1:
Input: arr = [1,3,4,2], mat = [[1,4],[2,3]] Output: 2 Explanation: The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].
Example 2:
Input: arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]] Output: 3 Explanation: The second column becomes fully painted at arr[3].
Constraints:
m == mat.lengthn = mat[i].lengtharr.length == m * n1 <= m, n <= 1051 <= m * n <= 1051 <= arr[i], mat[r][c] <= m * narr are unique.mat are unique.Problem summary: You are given a 0-indexed integer array arr, and an m x n integer matrix mat. arr and mat both contain all the integers in the range [1, m * n]. Go through each index i in arr starting from index 0 and paint the cell in mat containing the integer arr[i]. Return the smallest index i at which either a row or a column will be completely painted in mat.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,3,4,2] [[1,4],[2,3]]
[2,8,7,4,1,3,5,6,9] [[3,2,5],[1,4,6],[8,7,9]]
check-if-every-row-and-column-contains-all-numbers)difference-between-ones-and-zeros-in-row-and-column)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2661: First Completely Painted Row or Column
class Solution {
public int firstCompleteIndex(int[] arr, int[][] mat) {
int m = mat.length, n = mat[0].length;
Map<Integer, int[]> idx = new HashMap<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
idx.put(mat[i][j], new int[] {i, j});
}
}
int[] row = new int[m];
int[] col = new int[n];
for (int k = 0;; ++k) {
var x = idx.get(arr[k]);
int i = x[0], j = x[1];
++row[i];
++col[j];
if (row[i] == n || col[j] == m) {
return k;
}
}
}
}
// Accepted solution for LeetCode #2661: First Completely Painted Row or Column
func firstCompleteIndex(arr []int, mat [][]int) int {
m, n := len(mat), len(mat[0])
idx := map[int][2]int{}
for i := range mat {
for j := range mat[i] {
idx[mat[i][j]] = [2]int{i, j}
}
}
row := make([]int, m)
col := make([]int, n)
for k := 0; ; k++ {
x := idx[arr[k]]
i, j := x[0], x[1]
row[i]++
col[j]++
if row[i] == n || col[j] == m {
return k
}
}
}
# Accepted solution for LeetCode #2661: First Completely Painted Row or Column
class Solution:
def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
m, n = len(mat), len(mat[0])
idx = {}
for i in range(m):
for j in range(n):
idx[mat[i][j]] = (i, j)
row = [0] * m
col = [0] * n
for k in range(len(arr)):
i, j = idx[arr[k]]
row[i] += 1
col[j] += 1
if row[i] == n or col[j] == m:
return k
// Accepted solution for LeetCode #2661: First Completely Painted Row or Column
use std::collections::HashMap;
impl Solution {
pub fn first_complete_index(arr: Vec<i32>, mat: Vec<Vec<i32>>) -> i32 {
let m = mat.len();
let n = mat[0].len();
let mut idx = HashMap::new();
for i in 0..m {
for j in 0..n {
idx.insert(mat[i][j], [i, j]);
}
}
let mut row = vec![0; m];
let mut col = vec![0; n];
for k in 0..arr.len() {
let x = idx.get(&arr[k]).unwrap();
let i = x[0];
let j = x[1];
row[i] += 1;
col[j] += 1;
if row[i] == n || col[j] == m {
return k as i32;
}
}
-1
}
}
// Accepted solution for LeetCode #2661: First Completely Painted Row or Column
function firstCompleteIndex(arr: number[], mat: number[][]): number {
const m = mat.length;
const n = mat[0].length;
const idx: Map<number, number[]> = new Map();
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
idx.set(mat[i][j], [i, j]);
}
}
const row: number[] = Array(m).fill(0);
const col: number[] = Array(n).fill(0);
for (let k = 0; ; ++k) {
const [i, j] = idx.get(arr[k])!;
++row[i];
++col[j];
if (row[i] === n || col[j] === m) {
return k;
}
}
}
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.