Problem overview
The method countInGrid(grid, target) receives a grid of integers, an array of rows where each row is an array of integers, and a number target. It returns how many cells are equal to target.
For the grid [[1, 2, 3], [4, 2, 6], [2, 8, 2]] and target = 2, the value appears once in the first row, once in the second and twice in the third, so the answer is 4. For [[-1, 5], [5, -1]] and target = -1 the answer is 2: the two cells are in different rows, and each counts. For [[7, 8, 9]] and target = 4 no cell matches and the answer is 0.
Approach
This is the counting pattern, applied to a grid. Start a counter at 0, visit every cell, and add 1 whenever a cell equals the target. A grid needs two loops: the outer one visits the rows and the inner one visits the values in the current row. Together they reach every cell exactly once, so every match is counted exactly once.
Nothing is skipped and nothing is visited twice, so there is no need to track where a match was found. Only the count matters.
Step by step
- Declare
countwithletand start it at 0. It must belet, notconst, because it goes up inside the loops. - Loop over the rows.
for (let row of grid)picks one row at a time, from the top row down. Eachrowis an array of numbers. - Loop over the row's values.
for (let value of row)goes through the whole row before the outer loop moves on. - Compare and count. If
value === target,count++adds 1. Other cells are skipped. - Return
countafter both loops, so every cell is checked first.
The solution in JavaScript
class Solution {
countInGrid(grid, target) {
let count = 0;
for (let row of grid) {
for (let value of row) {
if (value === target) {
count++;
}
}
}
return count;
}
}
Details worth knowing:
===compares without converting. It is true only when both sides have the same type and value.2 === 2is true, and"2" === 2is false. The loose==would say"2" == 2is true, which is why===is the usual choice.count++is short forcount = count + 1.- No index is needed. The function only asks how many cells match, not where they are. When the position matters, an index loop is the tool, as in finding the first match.
Complexity
Time: O(r × c) for r rows and c columns, because each cell is compared once.
Space: O(1). Only one counter and the loop variables are used.
Edge cases and common mistakes
- No match. The counter never changes and the function returns 0.
- Negative targets.
-1is compared like any value.[[-1, 5], [5, -1]]withtarget = -1gives 2. - Stopping at the first match. Returning inside the loop would give 1 at most. This problem counts all matches.
- Returning inside the loops. Put
return countafter both loops close. - Using
=instead of===.if (value = target)assigns instead of comparing, which overwritesvalueand makes the test true for every cell unlesstargetis 0. - Counting rows instead of cells. A row with the target twice, like the third row of the first example, adds 2, not 1.
Testing
grid = [[1, 2, 3], [4, 2, 6], [2, 8, 2]],target = 2gives4.grid = [[-1, 5], [5, -1]],target = -1gives2.grid = [[7, 8, 9]],target = 4gives0.
