Problem overview
The method rowSums receives grid, an array of rows of integers, and returns an array holding the sum of each row, in the same order as the rows. A grid has between 1 and 10 rows, every row has the same number of values, and values may be negative.
For [[1, 2, 3], [4, 5, 6]] the answer is [6, 15]. For [[-1, -2], [-3, -4], [5, 5]] it is [-3, -7, 10]: one sum per row, top to bottom, and a row of negative numbers has a negative sum. When every row holds a single value, as in [[3], [1], [4]], each row's sum is that value, so the answer is [3, 1, 4].
Approach
This is the grid sum from a different angle. Instead of one total for the whole grid, every row needs its own total, which is saved when the row is finished. The structure has three parts: an outer loop over the rows, a fresh total at the start of each row, and an inner loop that adds up that row's values.
Where the total is created is the whole idea. If total were declared before the outer loop, it would keep growing across rows, and each saved number would include all the earlier rows. Declaring it inside the outer loop, before the inner one, resets it to 0 for every row.
Step by step
- Create an empty result array.
var sums = [Int]()starts with nothing and gets one number per row. - Loop over the rows.
for row in gridruns once per row, in order. Eachrowis an[Int]. - Start this row's total. Inside the loop,
var total = 0begins again for every row. - Loop over the row's values. A second
for value in rowloop, still inside the outer one, visits only the current row. - Add each value.
total += valuegoes inside the inner loop. - Save the row's sum. After the inner loop, but still inside the outer one,
sums.append(total). At that moment the whole row has been added. Appending at the end of each pass keeps the sums in row order. - Return
sumsafter the outer loop. A return inside it would give back only the first row's sum.
The solution in Swift
class Solution {
func rowSums(_ grid: [[Int]]) -> [Int] {
var sums = [Int]()
for row in grid {
var total = 0
for value in row {
total += value
}
sums.append(total)
}
return sums
}
}
Swift details worth knowing:
[Int]()builds an empty array of integers. It must be avar, sinceappendchanges the array. Aletarray can't be appended to, and the compiler says so.- Scope.
totalexists only inside the outer loop's braces. Each pass creates a new variable, which is why there is nothing to reset by hand. - Value semantics. The returned array is a separate value. Changing it later never changes
grid.
Complexity
Time is O(r * c) for r rows and c columns, because each cell is added once. Space is O(r) for the result array, which holds one number per row.
Edge cases and common mistakes
- Total declared outside the outer loop. The sums become running totals. For
[[1, 2, 3], [4, 5, 6]]the second entry would include the first row. - Appending in the wrong place. Putting
sums.append(total)inside the inner loop adds a number per cell, not per row. - Appending after the outer loop. Only the last row's total would survive, and only once.
- Single-column grids.
[[3], [1], [4]]works unchanged: each inner loop runs once. - Negative sums. They are correct as they are, as in
[-3, -7, 10].
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].
