Problem overview
The method countInGrid receives grid, an Array[Array[Int]], and an integer target. It returns how many cells of the grid are equal to target. A grid is an array of rows, and each row is an array of integers.
For the rows [1, 2, 3], [4, 2, 6] and [2, 8, 2] with target = 2, the 2 appears once in the first row, once in the second and twice in the third, so the answer is 4. For [-1, 5] and [5, -1] with target = -1 the answer is 2: the two matches are in different rows, and each counts. For [7, 8, 9] with target = 4 the answer is 0.
Approach: visit every cell once
There is no shortcut: the target can be in any cell, so every cell has to be looked at. Two nested loops do that. The outer loop goes through the rows, and the inner loop goes through the values of each row. Every cell is visited exactly once, and a counter goes up whenever a value equals the target.
This is the counting pattern from a flat array, with one more loop around it. The counter is created once, before both loops, because the answer covers the whole grid.
Step by step
- Create
var count = 0. It holds the number of matching cells found so far. It is avar, because it goes up inside the loops. - Loop over the rows with
for (row <- grid). Eachrowis anArray[Int]. - Loop over the values of the row with
for (value <- row), inside the first loop. The whole row is visited before the next row starts. - Compare and count. If
value == target, add 1 tocountwith+= 1. Cells with any other value change nothing. - Return
countafter both loops, so every cell has been checked first.
The solution in Scala
class Solution {
def countInGrid(grid: Array[Array[Int]], target: Int): Int = {
var count = 0
for (row <- grid) {
for (value <- row) {
if (value == target) {
count += 1
}
}
}
return count
}
}
Scala details worth knowing:
==compares values. For integers,value == targetis true when the numbers are equal. For other types, Scala's==also compares by value, not by reference.varversusval.count += 1iscount = count + 1, which needs avar. Declaringcountwithvalis a compile error.- Elements, not positions. Both loops give the elements directly, so no index arithmetic is needed. This lesson only asks how many cells match, not where they are.
- Negative targets. Nothing special:
-1is an ordinaryInt.
Complexity
With R rows and C columns, the comparison runs once per cell, so the time is O(R * C). The extra space is O(1): a counter and the loop variables.
Edge cases and common mistakes
- No match. The counter never changes and the method returns 0.
- Every cell matches. For a grid of 2 rows and 3 columns filled with the target, the answer is 6.
- Counting rows instead of cells. A row with two matches must add 2, not 1. The comparison is inside the inner loop for that reason.
- Resetting the counter. Creating
countinside the outer loop would keep only the last row's count. - Returning inside the loops. The method would stop before checking all the cells.
Testing
[[1, 2, 3], [4, 2, 6], [2, 8, 2]]with target2gives4.[[-1, 5], [5, -1]]with target-1gives2.[[7, 8, 9]]with target4gives0.
