Problem overview
The method diagonalSum receives a square grid of integers, with as many rows as columns, and returns the sum of the cells on the diagonal from the top-left corner to the bottom-right corner. A cell is on the diagonal when its row number and column number are the same: (0, 0), (1, 1), (2, 2) and so on.
For [[1, 2, 3], [4, 5, 6], [7, 8, 9]] the diagonal cells are 1, 5 and 9, so the answer is 15. For [[7]] the answer is 7, and for [[-1, 2], [3, -4]] it is -5, because only -1 and -4 are on the diagonal.
Approach: one index gives both coordinates
The earlier grid exercises used two nested loops to visit every cell. Here only n cells matter out of n * n, and each one has the same row and column number. A single counter i can supply both, so one loop is enough: at each turn, the cell is grid[i][i]. A second loop would visit every cell only to skip almost all of them.
The loop runs once per row, from 0 up to grid.length - 1. Because the grid is square, the same range of numbers is valid for the columns.
Step by step
- Declare
totalwithletand start it at 0. It isletbecause the loop changes it. - Loop with
i:for (let i = 0; i < grid.length; i++).grid.lengthis the number of rows, and it is also the number of columns. - Add the diagonal cell:
total += grid[i][i]. The first[i]picks rowi, and the second picks columniin that row. - Return
totalafter the loop. By then one cell per row has been added.
The solution in TypeScript
class Solution {
diagonalSum(grid: number[][]): number {
let total = 0;
for (let i = 0; i < grid.length; i++) {
total += grid[i][i];
}
return total;
}
}
Language details worth knowing:
- Chained indexing.
grid[i][i]is evaluated left to right:grid[i]is a row (anumber[]), and indexing that row with[i]gives a number. - Inferred types.
let total = 0is anumberwithout an annotation, andiis anumberas well. - Negative values.
total += -4subtracts 4. Nothing special is needed for them. - Annotations are erased.
number[][]helps the compiler and the editor. The compiled JavaScript has no types.
Complexity
Time: O(n) for an n by n grid, since the loop runs n times and each turn does one addition. Visiting all n * n cells would cost O(n * n). Space: O(1), a single total.
Edge cases and common mistakes
- One cell.
[[7]]runs the loop once and returns 7. - Only the diagonal counts. In
[[-1, 2], [3, -4]]the 2 and the 3 are ignored, and the answer is -5. - Nested loops. Two loops with an
if (row === col)inside give the same answer but do far more work. - Mixing up the other diagonal. The cells from the top-right corner to the bottom-left have a column number of
n - 1 - i, noti. That is a different sum from the one asked for here. - Returning inside the loop. It would add only the first diagonal cell.
- Non-square grids. The lesson only gives square grids. On a grid with fewer columns than rows,
grid[i][i]would eventually beundefinedand the total would becomeNaN.
Testing
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]gives15.[[7]]gives7.[[-1, 2], [3, -4]]gives-5.[[1, 2], [3, 4]]gives5.
