Set Matrix Zeroes
If element is 0, set entire row and column to 0. Classic in-place algorithm using first row/col as markers.
Visualization
Interactive visualization for Set Matrix Zeroes
Sample Matrix:
[[1, 1, 1], [1, 0, 1], [1, 1, 1]]
Interactive visualization with step-by-step execution
Implementation
1function setZeroes(matrix: number[][]): void {
2 const m = matrix.length, n = matrix[0].length;
3 let firstRowZero = false, firstColZero = false;
4
5 // Check first row
6 for (let j = 0; j < n; j++) {
7 if (matrix[0][j] === 0) firstRowZero = true;
8 }
9
10 // Check first col
11 for (let i = 0; i < m; i++) {
12 if (matrix[i][0] === 0) firstColZero = true;
13 }
14
15 // Mark zeros on first row/col
16 for (let i = 1; i < m; i++) {
17 for (let j = 1; j < n; j++) {
18 if (matrix[i][j] === 0) {
19 matrix[i][0] = 0;
20 matrix[0][j] = 0;
21 }
22 }
23 }
24
25 // Set zeros based on markers
26 for (let i = 1; i < m; i++) {
27 for (let j = 1; j < n; j++) {
28 if (matrix[i][0] === 0 || matrix[0][j] === 0) {
29 matrix[i][j] = 0;
30 }
31 }
32 }
33
34 // Handle first row
35 if (firstRowZero) {
36 for (let j = 0; j < n; j++) matrix[0][j] = 0;
37 }
38
39 // Handle first col
40 if (firstColZero) {
41 for (let i = 0; i < m; i++) matrix[i][0] = 0;
42 }
43}Deep Dive
Theoretical Foundation
O(1) space solution uses first row and col as markers. First pass: mark first row/col if zeros found. Track separately if first row/col themselves have zeros. Second pass: set zeros based on markers.
Complexity
Time
O(m×n)
O(m×n)
O(m×n)
Space
O(1)
Applications
Use Cases
Related Algorithms
Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.