Problem overview
The method gridSum receives grid, an Array[Array[Int]], and returns the sum of every number in it. A grid is a table: an array of rows, each row an array of integers, and all rows have the same length.
For the grid with rows [1, 2, 3] and [4, 5, 6] the first row adds up to 6 and the second to 15, so the answer is 21. For rows [-3, 4] and [5, -6] the answer is 0, since negative numbers simply make the sum smaller. A grid with one cell, [[7]], gives 7.
Approach: one loop inside another
Adding up a flat array takes one loop. A grid is an array of arrays, so it takes two. The outer loop visits the rows. For each row, the inner loop visits that row's values. Together they reach every cell exactly once, and each value is added to a running total.
The order doesn't matter for a sum, so the loops can go row by row without any special care. What matters is where the total lives: it is created once, before both loops, so that numbers from every row pile up in the same variable.
Step by step
- Create
var total = 0. Nothing has been added yet, so the sum so far is 0. It is avarbecause it changes. - Loop over the rows with
for (row <- grid). Each element of anArray[Array[Int]]is anArray[Int], one row of the grid. - Loop over the values of the row with
for (value <- row), inside the first loop. For each row, it goes through every value before the outer loop moves to the next row. - Add
valuetototalinside the inner loop. - Return
totalafter both loops, once every cell has been added.
The solution in Scala
class Solution {
def gridSum(grid: Array[Array[Int]]): Int = {
var total = 0
for (row <- grid) {
for (value <- row) {
total += value
}
}
return total
}
}
Scala details worth knowing:
Array[Array[Int]]. The type is an array whose elements are themselves arrays ofInt. Square brackets hold type parameters in Scala, not indexes.- Looping directly over elements.
for (row <- grid)gives the rows themselves, not their positions, so there are no index variables and no off-by-one mistakes. When you need positions, for example to read a column or a diagonal, loop overgrid.indicesinstead. +=on avar.total += valuemeanstotal = total + value.Intis 32 bits. Overflow wraps silently, but the constraints keep the sum tiny: at most 100 cells of at most 1000 each.
Complexity
With R rows and C columns, the inner body runs once per cell, so the time is O(R * C): the number of values in the grid, which every correct solution has to read. The extra space is O(1), just total and the loop variables.
Edge cases and common mistakes
- A single cell.
[[7]]runs each loop once and returns 7. - Negative values. They are added like any other number: for
[[-3, 4], [5, -6]]the result is 0. - Creating
totalinside the outer loop. It would reset to 0 for every row and the method would return only the last row's sum. - Returning inside a loop. The method would end after the first row or the first value, and part of the grid would never be added.
- Declaring
totalwithval. The compiler rejectstotal += valueon aval.
Testing
[[1, 2, 3], [4, 5, 6]]gives21.[[-3, 4], [5, -6]]gives0.[[7]]gives7.
