Problem overview
The method countInGrid receives a grid of integers, an array of rows, and an integer target. It returns how many cells of the grid are equal to target. Values and the target can be negative.
For a grid with rows [1, 2, 3], [4, 2, 6] and [2, 8, 2] and target = 2, the answer is 4: one 2 in the first row, one in the second and two in the third. For [[-1, 5], [5, -1]] and target = -1 the answer is 2, and for [[7, 8, 9]] and target = 4 it is 0.
Approach: visit every cell and count the matches
There is no shortcut: the target can be in any cell, so every cell must be looked at. Two nested loops do it. The outer loop takes the rows one at a time, and the inner loop goes through the whole current row before the outer loop moves on. Each cell is visited exactly once, and a counter goes up by one for each cell that equals the target.
The counter is created before the loops so it keeps its value from one cell to the next, and the function returns it after both loops, once every cell has been checked.
Compared with finding a value, counting never stops early. A search can return at the first match, but a count has to see the last cell, because the last cell may be one more match. That is why the return sits after the loops and the loops have no break.
Step by step
- Declare
countwithletand start it at 0. It islet, notconst, because it changes inside the loops. - Loop over the rows:
for (const row of grid). Anumber[][]is an array of rows, so eachrowis anumber[]. - Loop over the values of the row:
for (const value of row), inside the first loop. - Compare and count. If
value === target, add 1 withcount++. Other values are skipped. - Return
countafter both loops.
The solution in TypeScript
class Solution {
countInGrid(grid: number[][], target: number): number {
let count = 0;
for (const row of grid) {
for (const value of row) {
if (value === target) {
count++;
}
}
}
return count;
}
}
Language details worth knowing:
===. It compares two values without converting types. For numbers it is the right test for "equal". Negative numbers need nothing special:-1 === -1is true.count++. It adds 1 tocount. It is shorthand forcount += 1.- Inferred type.
let count = 0has the typenumberwithout an annotation, because the compiler infers it from the starting value. - Nested
for...of. The loop variablesrowandvalueare declared withconst: they are new on each pass and never reassigned.
Complexity
Time: O(R * C) for R rows and C columns, one comparison per cell. Space: O(1), a single counter.
Edge cases and common mistakes
- Matches in different rows. Every one counts.
[[-1, 5], [5, -1]]with target -1 gives 2. - No match. The counter never moves, and the function returns 0.
- Returning inside the loops. A
returnthere would end the function at the first cell or the first row, before the count is complete. - Using
constfor the counter. Changing it later withcount++would be an error. - Stopping at the first match. This lesson counts every match. Finding only the first one is a different problem.
Testing
[[1, 2, 3], [4, 2, 6], [2, 8, 2]]with target2gives4.[[-1, 5], [5, -1]]with target-1gives2.[[7, 8, 9]]with target4gives0.
