Problem overview
The method gridSum receives grid, a table of integers stored as an array of rows (number[][]), and returns the sum of all its numbers. The grid has between 1 and 10 rows, every row has the same number of values, and the values can be negative.
For [[1, 2, 3], [4, 5, 6]] the first row adds up to 6 and the second to 15, so the answer is 21. For [[-3, 4], [5, -6]] the answer is 0, and a grid with a single number, [[7]], gives 7.
Approach: a loop inside a loop
A grid is an array that contains arrays. Going through a plain array takes one loop; going through every cell of a grid takes two. The outer loop picks the rows one at a time. For each row, the inner loop visits all of its values before the outer loop moves on. Together the loops visit every cell exactly once, so adding each value to a running total gives the sum of the whole grid.
The running total is created before the loops and returned after them. It must live outside both loops: if it were declared inside the outer loop, it would start again from 0 for every row.
Step by step
- Create
totalwithletand start it at 0. No number has been added yet. It isletbecause its value changes. - Loop over the rows with
for (const row of grid). Each item ofgridis itself an array of numbers, sorowis anumber[]. - Loop over one row with
for (const value of row), inside the first loop. For every row, this loop runs once per value. - Add the value:
total += value. Negative values make the total smaller, which needs no special handling. - Return
totalafter both loops. Returning inside a loop would stop after the first row or the first value.
The solution in TypeScript
class Solution {
gridSum(grid: number[][]): number {
let total = 0;
for (const row of grid) {
for (const value of row) {
total += value;
}
}
return total;
}
}
Language details worth knowing:
for...ofgives values. It hands you each item of an array directly, with no index to manage. The loop variable is declared withconstbecause each turn gets a fresh one that is never reassigned.- No annotation on
total. TypeScript infersnumberfrom the starting value 0, solet total = 0is the same aslet total: number = 0. number[][]. It means an array whose items are arrays of numbers. Like all annotations it is erased when the code is compiled to JavaScript.+=.total += valueis shorthand fortotal = total + value.
Complexity
Time: O(R * C) for a grid with R rows and C columns, because each of the R * C cells is visited once. Space: O(1): only total is stored besides the input.
Edge cases and common mistakes
- One cell.
[[7]]runs both loops once and returns 7. - Cancelling values.
[[-3, 4], [5, -6]]returns 0. - Declaring
totalin the wrong place. Inside the outer loop it resets for each row, and the function can't see it after the loops. - Using
for...in. On an array it gives the indices as strings, so adding them would join text instead of adding numbers. - Returning too early. A
returninside a loop adds only part of the grid.
Testing
[[1, 2, 3], [4, 5, 6]]gives21.[[-3, 4], [5, -6]]gives0.[[7]]gives7.
