Start learning free

LESSON 258 OF 289 · NESTED LOOPS OVER GRIDS

Row Sums in TypeScript: One Total per Row

Practice Read the full explanation

Given a grid of integers grid, return an array with the sum of each row, in the same order as the rows.

A grid is a table of numbers made of rows. Every row has the same number of values, one in each column. Rows are counted from the top and columns from the left, both starting at 0.

 

Example 1:

Input: grid = [[1, 2, 3],
               [4, 5, 6]]
Output: [6, 15]
Explanation: Row 0 adds up to 1 + 2 + 3 = 6 and row 1 to 4 + 5 + 6 = 15.

Example 2:

Input: grid = [[-1, -2],
               [-3, -4],
               [5, 5]]
Output: [-3, -7, 10]
Explanation: One sum per row, top to bottom. A row of negative numbers has a negative sum.

Example 3:

Input: grid = [[3],
               [1],
               [4]]
Output: [3, 1, 4]
Explanation: Every row has a single value, so each row's sum is that value.

 

Constraints:

  • grid has at least 1 row and at most 10 rows.
  • Every row has the same number of values, at least 1 and at most 10.
  • Every value is an integer between -1000 and 1000.
STEP 1 OF 7
    Ctrl + Enter to check

    TYPESCRIPT · NESTED LOOPS OVER GRIDS

    Row Sums in TypeScript, explained

    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

    1. Create sums as an empty number[] with let. It collects one sum per row.
    2. Loop over the rows with for (const row of grid). It runs once per row, top to bottom.
    3. Create total at 0 inside that loop. It is a new variable on every pass, so one row's numbers never mix with the next row's.
    4. Loop over the values of the current row with for (const value of row).
    5. Add the value to total.
    6. Push total onto sums after the inner loop, still inside the outer one. The row is finished, so total is its sum.
    7. Return sums after 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 let inside a loop body exists only in that body. Each pass of the outer loop gets a new total, 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 total before 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].
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free