Problem overview
ColumnSums receives grid, an int[][], and returns a List<int> with the sum of each column, from left to right. Column c is made of the value at position c in every row.
For the rows [1, 2, 3] and [4, 5, 6], column 0 holds 1 and 4, column 1 holds 2 and 5, and column 2 holds 3 and 6, so the result is [5, 7, 9]. For [2, -1], [3, -1] and [-5, 7] it is [0, 5]. A grid with the one row [4, -2, 9] gives [4, -2, 9], because each column holds a single number.
Approach: columns on the outside
A jagged array is a list of rows, so rows are easy to reach: foreach gives them one at a time. Columns are not stored anywhere. To sum a column you must reach into every row at the same position. That needs indexes, so this lesson swaps foreach for for loops.
The nesting is the reverse of Row Sums. The outer loop picks a column, and the inner loop walks down the rows, adding the value where that row meets the column. When the inner loop ends, the column is complete. Because the outer loop goes through the columns from left to right, the sums come out in column order.
Step by step
- Create an empty
List<int>namedsums. It collects one sum per column. - Loop over column numbers with
for (int col = 0; col < grid[0].Length; col++). Every row has one value per column, so the length of the first row,grid[0].Length, is the number of columns. - Create
int total = 0inside the loop. It starts again at 0 for every column, so one column's numbers never mix into the next. - Write the inner loop over row numbers:
for (int row = 0; row < grid.Length; row++).grid.Lengthis the number of rows, and a column has one number in each. - Add
grid[row][col]tototal.grid[row]is a whole row, and[col]picks this column's value out of it. - After the inner loop, still inside the outer one,
sums.Add(total). Every row's number in this column has been added by then. - Return
sumsafter the outer loop, not inside it, or only the first column would come back.
The solution in C#
class Solution {
public List<int> ColumnSums(int[][] grid) {
List<int> sums = new List<int>();
for (int col = 0; col < grid[0].Length; col++) {
int total = 0;
for (int row = 0; row < grid.Length; row++) {
total += grid[row][col];
}
sums.Add(total);
}
return sums;
}
}
C# details:
- Row first, then column. In
grid[row][col]the first brackets pick a row (anint[]) and the second pick a position in it. Swapping them reads the wrong cell, and on a grid that is not square it can go out of range. - Out of range throws. Reading
grid[3][0]on a grid with three rows throwsIndexOutOfRangeException. Arrays are checked on every access, so a wrong bound crashes loudly instead of reading garbage. - The first row sets the width.
grid[0].Lengthis safe here because the grid always has at least one row. On an empty array of rows (new int[0][]),grid[0]itself throwsIndexOutOfRangeException. - Length versus Count. Arrays have
Length; aList<int>hasCount. The grid is made of arrays, so the loops useLength. List<T>lives inSystem.Collections.Generic; in a file of your own, addusing System.Collections.Generic;.
Edge cases and common mistakes
- Mixing up the two bounds. The columns loop is bounded by
grid[0].Lengthand the rows loop bygrid.Length. Using one for both only works on square grids and fails on, say, 2 rows by 3 columns. - Not resetting
total. Declared before the outer loop, it would carry each column's sum into the next. - One row. The inner loop runs once per column, so each sum is that single value.
- Negative values.
[[2, -1], [3, -1], [-5, 7]]gives[0, 5], so a column can sum to 0.
Testing
grid | Result |
|---|---|
[[1, 2, 3], [4, 5, 6]] | [5, 7, 9] |
[[2, -1], [3, -1], [-5, 7]] | [0, 5] |
[[4, -2, 9]] | [4, -2, 9] |
