Problem overview
CountInGrid receives a grid, an int[][] (an array of rows, each row an array of integers), and an int target. It returns how many cells of the grid are equal to target.
In the grid with rows [1, 2, 3], [4, 2, 6] and [2, 8, 2], the target 2 appears once in the first row, once in the second and twice in the third: 1 + 1 + 2 = 4. In the grid [-1, 5], [5, -1] with target -1, the answer is 2: the two cells are in different rows, and each one counts. In the one-row grid [7, 8, 9] with target 4, nothing matches and the answer is 0.
Approach: visit every cell once
Nothing about the problem lets you skip a cell. A matching value can be anywhere, in any row, so the method has to look at all of them. The standard way to visit every cell of a jagged array is a loop over the rows with a loop over the values of each row inside it. Together they reach each cell exactly once.
For each cell, the only question is whether it equals target. A counter that starts at 0 grows by 1 for every yes. The order of the visits doesn't matter, because adding 1 gives the same total in any order.
Step by step
- Create
int count = 0. No cell has been checked, so no match has been found. - Loop over the rows with
foreach (int[] row in grid). Anint[][]is an array of rows, so each item is anint[]. The outer loop takes one row at a time, from the top. - Inside it, loop over that row's values with
foreach (int value in row). The inner loop goes through a whole row before the outer loop moves on, so every cell is visited once. - Compare with
==: ifvalue == target, docount++. Cells with any other value are skipped. - Return
countafter both loops, so every cell is checked first.
The solution in C#
class Solution {
public int CountInGrid(int[][] grid, int target) {
int count = 0;
foreach (int[] row in grid) {
foreach (int value in row) {
if (value == target) {
count++;
}
}
}
return count;
}
}
C# details:
- One equals sign or two.
=assigns and==compares. Writingif (value = target)doesn't compile in C#, because anintcan't be used as a condition. That is a safety net other languages lack. count++. This adds 1 tocount, the same ascount += 1.- Jagged versus rectangular. The lesson's grid type is
int[][], an array of arrays, so the loops areforeachover rows and then values. A rectangularint[,]is a different type: aforeachover it visits every cell directly, without rows. - No index is needed. The answer is a count of matches, so the position of a match is not used here. The next lesson, finding a cell, does need it.
Edge cases and common mistakes
- Returning inside a loop. A
return count;inside the loops would end the method after the first cell. - Counting rows instead of cells. A row such as
[2, 8, 2]contains the target twice. Counting once per row would give 3 for the first example, not 4. The inner loop counts each cell separately. - Negative targets.
==compares negative numbers like any others. The target -1 on the second example returns 2. - No match. The counter never changes, and the method returns 0.
- Forgetting the braces. Without
{ }, anifcontrols only the next statement. Keeping the braces makes it clear thatcount++is what runs on a match.
Testing
grid | target | Result |
|---|---|---|
[[1, 2, 3], [4, 2, 6], [2, 8, 2]] | 2 | 4 |
[[-1, 5], [5, -1]] | -1 | 2 |
[[7, 8, 9]] | 4 | 0 |
