Arrays And Strings
Matrix Traversal
Walk a matrix row by row, visiting every cell exactly once while maintaining a running total.
O(rows * cols) time / O(1) space
cells: 9running total: 0
Index 0C
1Index 1
2Index 2R
3Index 3
4Index 4
5Index 5
6Index 6
7Index 7
8Index 8
9Time
O(1)
Space
O(1)
Step Note
Traverse a 3 x 3 matrix row by row.
function rowWiseTraversal(matrix: number[][]) {
let total = 0
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
total += matrix[row][col]
}
}
return total
}