Problem overview
The method rowSums(grid) receives a grid of integers, an array of rows, and returns an array with the sum of each row, in the same order as the rows. A grid has the same number of values in every row, counted from the top row and the left column, both from 0.
For [[1, 2, 3], [4, 5, 6]] row 0 adds up to 1 + 2 + 3 = 6 and row 1 to 4 + 5 + 6 = 15, so the result is [6, 15]. For [[-1, -2], [-3, -4], [5, 5]] the result is [-3, -7, 10]: a row of negative numbers has a negative sum. For [[3], [1], [4]] every row has one value, so each sum is that value and the result is [3, 1, 4].
Approach
The result has one number per row, so the outer loop goes over the rows and produces one sum on each turn. For each row you need a fresh running total, an inner loop that adds the row's values to it, and, when the row is done, a push of the finished total into the result.
Compare this with a plain grid sum, which has a single total outside both loops. Here the total belongs to one row, so it is created inside the outer loop and starts again at 0 each time. That placement is the key idea of the lesson.
Step by step
- Create
sumsas an empty array. The sums are added one at a time, so it starts empty. - Loop over the rows with
for (let row of grid). Each row produces exactly one sum, so the loop runs once per row. - Create
totalinside the loop and set it to 0. It restarts for every row, so one row's numbers never mix into the next row's sum. - Loop over the row's values with
for (let value of row). It visits the values of the current row and nothing else. - Add each value with
total += value. - Push the total after the inner loop, still inside the outer one. At that point the whole row has been added, and pushing at the end keeps the sums in row order.
- Return
sumsafter the outer loop. Returning inside it would give back only the first row's sum.
The solution in JavaScript
class Solution {
rowSums(grid) {
let sums = [];
for (let row of grid) {
let total = 0;
for (let value of row) {
total += value;
}
sums.push(total);
}
return sums;
}
}
Details worth knowing:
- Where a variable is declared decides how long it lives. A
letinside the loop body is created anew on every turn, sototalis 0 at the start of each row.sums, declared before the loop, lives for the whole function. pushappends and keeps order. It adds the new value at the end of the array, so the sum of row 0 lands at index 0, row 1 at index 1, and so on.- The result is a new array.
gridis only read, never modified.
Complexity
Time: O(r × c) for r rows and c columns. Every cell is visited once.
Space: O(r) for the result, which holds one number per row. Beyond that, the function uses a few variables.
Edge cases and common mistakes
- Declaring
totaloutside the outer loop. The sums then run on from row to row. For[[1, 2, 3], [4, 5, 6]]that mistake gives[6, 21]instead of[6, 15], because the second row starts from 6. - Pushing inside the inner loop. It adds one partial total per value instead of one sum per row.
- Returning inside the outer loop. Only the first row's sum comes back.
- One value per row.
[[3], [1], [4]]gives[3, 1, 4]: each inner loop runs once. - Negative values.
[[-1, -2], [-3, -4], [5, 5]]gives[-3, -7, 10]. They are added like any number.
Testing
grid = [[1, 2, 3], [4, 5, 6]]gives[6, 15].grid = [[-1, -2], [-3, -4], [5, 5]]gives[-3, -7, 10].grid = [[3], [1], [4]]gives[3, 1, 4].
