Problem overview
The method gridSum(grid) receives a grid of integers and returns the sum of every number in it. In JavaScript a grid is an array of rows, and each row is an array of numbers. Rows are counted from the top and columns from the left, both from 0. Every row has the same number of values.
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, because negative numbers make the sum smaller. For [[7]] the grid has one cell, and the answer is 7.
Approach
Adding up an ordinary array takes one loop: visit each value and add it to a running total. A grid is an array whose items are themselves arrays, so one loop is not enough. The outer loop visits the rows, and for each row an inner loop visits that row's values. Together the two loops reach every cell exactly once, and every cell is added to the same total.
The order of the cells doesn't matter for a sum, so the answer comes out the same whether you walk the grid by rows or by columns. Walking by rows is the natural way, because a row is what the outer array hands you.
Step by step
- Create
totalwithletand start it at 0. It holds the sum of the numbers added so far, and no number has been added yet. - Loop over the rows.
for (let row of grid)gives one row per turn, from the top row to the bottom one. Eachrowis an array of integers. - Loop over the values of the row. Inside the outer loop,
for (let value of row)visits every value of the current row before the outer loop moves on. - Add the value.
total += valueis short fortotal = total + value. Negative values are added like any others, so they lower the total. - Return
totalafter both loops. Areturninside a loop would end the function after the first row or the first value.
The solution in JavaScript
class Solution {
gridSum(grid) {
let total = 0;
for (let row of grid) {
for (let value of row) {
total += value;
}
}
return total;
}
}
Details worth knowing:
for...ofgives values, not positions. This lesson never needs the row or column number, which is whyfor...ofis enough. When a position matters, as in finding a cell, you need an index loop instead.letfor the total, and for the loop variables.totalis reassigned on every addition, so it can't beconst. The loop variablesrowandvalueare new bindings on every turn, andconstwould also work for them.- A row is an array.
rowin the outer loop is the whole array, for example[1, 2, 3], not a number. Adding it directly to the total would turn the total into a string.
Complexity
Time: O(r × c) for r rows and c columns, which is the number of cells. Each cell is visited once and added in constant time.
Space: O(1). The function keeps one number and the loop variables, however large the grid is.
Edge cases and common mistakes
- A single cell.
[[7]]makes each loop run once and returns 7. - Negative numbers.
[[-3, 4], [5, -6]]gives 0. Nothing special is needed, but a total that started at a large number instead of 0 would break such cases. - Resetting the total. The total belongs outside both loops. If
let total = 0were inside the outer loop, each row would start over and the function would return only the last row's sum. - Returning too early. Put
return totalafter both loops close. - Looping over the grid once.
for (let value of grid)gives rows, not numbers. Adding an array to a number produces a string: for[[1, 2, 3], [4, 5, 6]]the "total" ends as"01,2,34,5,6", not a sum.
Testing
grid = [[1, 2, 3], [4, 5, 6]]gives21.grid = [[-3, 4], [5, -6]]gives0.grid = [[7]]gives7.
