Problem overview
The method gridSum receives grid, an array of rows where each row is an array of integers, and returns the sum of every number in it. All rows have the same length. The values can be negative.
For [[1, 2, 3], [4, 5, 6]] the first row adds up to 6 and the second to 15, so the answer is 21. For [[-3, 4], [5, -6]] the answer is 0, because the negative numbers cancel the positive ones. A grid with one cell, [[7]], gives 7.
Approach
A grid is an array of arrays, so reaching every cell takes two loops, one inside the other. The outer loop picks a row. The inner loop walks through the values of that row. Together they visit each cell exactly once, and a running total collects the values.
The type [[Int]] explains why this works. Looping over a [[Int]] hands out its elements, and each element is an [Int]: a whole row. Looping over that row hands out plain Int values. There is no index arithmetic to get wrong.
Step by step
- Create
totalwithvar, starting at 0. No number has been added yet, so 0 is the right start, andvaris needed because the value changes. - Loop over the rows.
for row in gridruns once per row, from the top one down. Close the loop's brace right away and put the next step inside it. - Loop over the values of the row.
for value in rowsits inside the first loop. For every row, it runs through all of that row's values before the outer loop moves on. - Add the value.
total += valuegoes inside the inner loop, so every cell is added once, negative ones included. - Return
totalafter both loops. Returning inside a loop would add only part of the grid.
The solution in Swift
class Solution {
func gridSum(_ grid: [[Int]]) -> Int {
var total = 0
for row in grid {
for value in row {
total += value
}
}
return total
}
}
Swift details worth knowing:
- Loop variables are constants.
rowandvalueare fresh constants on every turn. You can read them but not assign to them. [[Int]]is an array of arrays. Arrays are value types, sorowis a copy of the row, taken cheaply because Swift copies the storage only if someone modifies it. Nothing in this loop changesgrid.Intis 64 bits. With values between -1000 and 1000 and at most 100 cells, the sum is far from overflowing. Swift traps on overflow instead of wrapping, but it is not a concern here.
Complexity
Time is O(r * c) for a grid with r rows and c columns: one addition per cell. Space is O(1), because only total is stored besides the input.
Edge cases and common mistakes
- Single cell.
[[7]]runs each loop once and returns 7. - Negative values. They are added like any other number.
[[-3, 4], [5, -6]]gives 0. - Returning too early. A
return totalinside either loop adds only the first cell or the first row instead of the whole grid. - Declaring
totalinside a loop. It would start again at 0 on every row, and the final value would be out of scope for thereturn. - Using one loop.
for value in gridhands out rows, not numbers, sototal += valueis a type error: an[Int]can't be added to anInt.
Testing
[[1, 2, 3], [4, 5, 6]]gives 21.[[-3, 4], [5, -6]]gives 0.[[7]]gives 7.
