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.
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
You must do it in place.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Constraints:
m == matrix.lengthn == matrix[0].length1 <= m, n <= 200-231 <= matrix[i][j] <= 231 - 1Follow up:
O(mn) space is probably a bad idea.O(m + n) space, but still not the best solution.Problem summary: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,1,1],[1,0,1],[1,1,1]]
[[0,1,2,0],[3,4,5,2],[1,3,1,5]]
game-of-life)number-of-laser-beams-in-a-bank)minimum-operations-to-remove-adjacent-ones-in-matrix)remove-all-ones-with-row-and-column-flips-ii)class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean firstColZero = false;
for (int r = 0; r < m; r++) {
if (matrix[r][0] == 0) firstColZero = true;
for (int c = 1; c < n; c++) {
if (matrix[r][c] == 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
if (matrix[r][0] == 0 || matrix[0][c] == 0) matrix[r][c] = 0;
}
}
if (matrix[0][0] == 0) {
for (int c = 0; c < n; c++) matrix[0][c] = 0;
}
if (firstColZero) {
for (int r = 0; r < m; r++) matrix[r][0] = 0;
}
}
}
func setZeroes(matrix [][]int) {
m, n := len(matrix), len(matrix[0])
firstColZero := false
for r := 0; r < m; r++ {
if matrix[r][0] == 0 {
firstColZero = true
}
for c := 1; c < n; c++ {
if matrix[r][c] == 0 {
matrix[r][0] = 0
matrix[0][c] = 0
}
}
}
for r := 1; r < m; r++ {
for c := 1; c < n; c++ {
if matrix[r][0] == 0 || matrix[0][c] == 0 {
matrix[r][c] = 0
}
}
}
if matrix[0][0] == 0 {
for c := 0; c < n; c++ {
matrix[0][c] = 0
}
}
if firstColZero {
for r := 0; r < m; r++ {
matrix[r][0] = 0
}
}
}
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
m, n = len(matrix), len(matrix[0])
first_col_zero = False
for r in range(m):
if matrix[r][0] == 0:
first_col_zero = True
for c in range(1, n):
if matrix[r][c] == 0:
matrix[r][0] = 0
matrix[0][c] = 0
for r in range(1, m):
for c in range(1, n):
if matrix[r][0] == 0 or matrix[0][c] == 0:
matrix[r][c] = 0
if matrix[0][0] == 0:
for c in range(n):
matrix[0][c] = 0
if first_col_zero:
for r in range(m):
matrix[r][0] = 0
impl Solution {
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let m = matrix.len();
let n = matrix[0].len();
let mut first_col_zero = false;
for r in 0..m {
if matrix[r][0] == 0 {
first_col_zero = true;
}
for c in 1..n {
if matrix[r][c] == 0 {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for r in 1..m {
for c in 1..n {
if matrix[r][0] == 0 || matrix[0][c] == 0 {
matrix[r][c] = 0;
}
}
}
if matrix[0][0] == 0 {
for c in 0..n {
matrix[0][c] = 0;
}
}
if first_col_zero {
for r in 0..m {
matrix[r][0] = 0;
}
}
}
}
function setZeroes(matrix: number[][]): void {
const m = matrix.length;
const n = matrix[0].length;
let firstColZero = false;
for (let r = 0; r < m; r++) {
if (matrix[r][0] === 0) firstColZero = true;
for (let c = 1; c < n; c++) {
if (matrix[r][c] === 0) {
matrix[r][0] = 0;
matrix[0][c] = 0;
}
}
}
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) {
if (matrix[r][0] === 0 || matrix[0][c] === 0) matrix[r][c] = 0;
}
}
if (matrix[0][0] === 0) {
for (let c = 0; c < n; c++) matrix[0][c] = 0;
}
if (firstColZero) {
for (let r = 0; r < m; r++) matrix[r][0] = 0;
}
}
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.