Problem overview
The method rowSums receives grid, an array of rows (number[][]), and returns an array with the sum of each row, in row order. The grid has 1 to 10 rows, every row has the same number of values, and values can be negative.
For [[1, 2, 3], [4, 5, 6]] the result is [6, 15]. For [[-1, -2], [-3, -4], [5, 5]] it is [-3, -7, 10], and for a grid with one value per row, [[3], [1], [4]], it is [3, 1, 4].
Approach: reset the total for every row
This is the grid-sum idea with one change. Summing the whole grid needs one total for everything. Here every row needs its own total, so the total is created inside the outer loop, where it starts again at 0 for each row. When the inner loop finishes a row, that row's total is complete and goes into the result array. Pushing at the end of each pass keeps the sums in the same order as the rows.
The result array, sums, is the only thing created before the loops, since it must survive all of them.
Step by step
- Create
sumsas an emptynumber[]withlet. It collects one sum per row. - Loop over the rows with
for (const row of grid). It runs once per row, top to bottom. - Create
totalat 0 inside that loop. It is a new variable on every pass, so one row's numbers never mix with the next row's. - Loop over the values of the current row with
for (const value of row). - Add the value to
total. - Push
totalontosumsafter the inner loop, still inside the outer one. The row is finished, sototalis its sum. - Return
sumsafter both loops.
The solution in TypeScript
class Solution {
rowSums(grid: number[][]): number[] {
let sums: number[] = [];
for (const row of grid) {
let total = 0;
for (const value of row) {
total += value;
}
sums.push(total);
}
return sums;
}
}
Language details worth knowing:
- Scope. A variable declared with
letinside a loop body exists only in that body. Each pass of the outer loop gets a newtotal, which is what makes the reset automatic. No line of code sets it back to 0. push. It appends to the end of an array and changes the array in place. Items stay in the order they were added.- The annotation on
sums.number[]tells the compiler what the array holds. It is removed from the compiled JavaScript. - Return type. The method returns
number[], an array, not a single number.
Complexity
Time: O(R * C) for R rows and C columns, since each cell is added once. Space: O(R) for the result, one number per row.
Edge cases and common mistakes
- Declaring
totalbefore the outer loop. The sums would then be running totals: for the first example the result would be[6, 21]instead of[6, 15]. - Forgetting the push or putting it inside the inner loop. Inside the inner loop it would push a partial total after every value.
- Returning inside the loop. Only the first row's sum would come back.
- A single column.
[[3], [1], [4]]gives[3, 1, 4], since each row has one value. - Negative rows. A row of negative values has a negative sum, with no special handling.
Testing
[[1, 2, 3], [4, 5, 6]]gives[6, 15].[[-1, -2], [-3, -4], [5, 5]]gives[-3, -7, 10].[[3], [1], [4]]gives[3, 1, 4].
