Problem overview
The function countInGrid receives grid, a vector<vector<int>>, and an integer target. It returns how many cells of the grid are equal to target. The grid is a vector of rows, and each row is a vector of integers.
In [[1, 2, 3], [4, 2, 6], [2, 8, 2]] with target = 2, the value 2 appears once in the first row, once in the second and twice in the third, so the answer is 4. In [[-1, 5], [5, -1]] with target = -1 the answer is 2: the two cells are in different rows, and each counts. In [[7, 8, 9]] with target = 4 no cell matches, so the answer is 0.
Approach: visit every cell, count the matches
Nothing about the grid's layout helps here, because a match can be anywhere. The function has to look at every cell once. Two nested loops do that: the outer one picks a row, and the inner one goes through the values of that row. A counter starts at 0 and goes up by one every time a value equals target.
The counter belongs outside both loops, because the answer covers the whole grid and not a single row. Following the first example, count stays 0 for the first value, 1, and then rises to 1 at the 2 in row 0, to 2 in row 1, and to 3 and 4 for the two 2s in row 2. The function returns 4.
Step by step
- Create
countas anintset to 0. No match has been found yet. - Loop over the rows with a range-based
forand avector<int>namedrow. Each element of the grid is one row. - Loop over the values of the row with an
intnamedvalue. Together the two loops visit every cell exactly once. - Compare with the target. If
value == target, docount++. Other values are skipped. - Return
countafter both loops. Returning inside them would stop before every cell had been checked.
The solution in C++
class Solution {
public:
int countInGrid(vector<vector<int>> &grid, int target) {
int count = 0;
for (vector<int> row : grid) {
for (int value : row) {
if (value == target) {
count++;
}
}
}
return count;
}
};
C++ details:
==versus=.value == targetcompares. Writingvalue = targetwould assign, change the loop's copy, and make the condition true whenevertargetis not 0. The compiler accepts it, so watch for it.- Rows are copied.
vector<int> rowmakes a copy of each row. The function only reads, so the result is the same, andconst vector<int>& rowavoids the copies. - Negative targets. Nothing special is needed:
-1 == -1is true like any other equal pair. - Increment.
count++adds 1, the same ascount += 1.
Complexity
For R rows and C columns, the function makes R * C comparisons, so the time is O(R * C). The extra memory is constant, apart from the row copy, because only the counter is stored. Looking at every cell can't be avoided for an unsorted grid, so this is the best possible.
Edge cases and common mistakes
- No match.
[[7, 8, 9]]with target 4 returns 0, the value the counter started with. - Counting rows instead of cells. A row with two matches adds 2, as in the last row of the first example, not 1.
- Counter inside the outer loop. It would restart for each row and return only the last row's count, 2 instead of 4 for the first example.
- Returning inside the loops. The function would stop after the first match or the first row.
- Negative numbers. A target of -1 matches only -1, not 1.
Testing
[[1, 2, 3], [4, 2, 6], [2, 8, 2]]withtarget = 2gives 4.[[-1, 5], [5, -1]]withtarget = -1gives 2.[[7, 8, 9]]withtarget = 4gives 0.
