Problem overview
count_in_grid receives a grid, a Vec<Vec<i32>> of rows, and an i32 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 2 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]] with target = -1 the answer is 2, and for [[7, 8, 9]] with target = 4 it is 0.
Approach: visit every cell and count the matches
There is no shortcut: the grid is not sorted, so any cell can be a match and every one must be looked at. An outer loop goes through the rows and an inner loop through the values of the current row. Each value is compared with target, and a match adds 1 to a counter.
Step by step
- Create the counter.
let mut count = 0;holds the matches found so far. It ismutbecause it grows inside the loops. - Loop over the rows.
for row in &gridborrows each row, aVec<i32>, without taking it out of the grid. - Loop over the values of the row.
for value in rowgoes through the whole row before the outer loop moves on, so every cell is visited exactly once. - Compare.
if *value == targetchecks the cell. On a match,count += 1. - Return after the loops, when every cell has been checked.
The solution in Rust
impl Solution {
fn count_in_grid(grid: Vec<Vec<i32>>, target: i32) -> i32 {
let mut count = 0;
for row in &grid {
for value in row {
if *value == target {
count += 1;
}
}
}
return count;
}
}
Rust details worth knowing:
- References and
*. Because the loops borrow,valueis a&i32, a reference to the number.targetis ani32. Rust has no==between a reference and a plain number:value == targetfails withcan't compare `&{integer}` with `{integer}`. Writing*valuereads the number the reference points to. - The type of
count. It starts as a bare0, and the return type makes it ani32. - No index arithmetic. Looping over the values themselves avoids indices, so there is no way to go out of bounds here.
How the loops move
For the first example the outer loop starts with the row [1, 2, 3], and the inner loop compares 1, 2 and 3 with the target 2, finding one match. Then the outer loop moves to [4, 2, 6] for one more, and finally to [2, 8, 2] for two. The counter goes 1, 2, 4, and that is the value returned. The counter belongs before the outer loop, so it keeps its value from one row to the next.
Complexity
Time: O(r * c) for r rows and c columns, because each cell is compared once. Space: O(1), since only count and the loop variables are stored.
Edge cases and common mistakes
- No match. The counter never changes and the function returns 0.
- Every cell matches. For a 2 by 2 grid of 3s with
target = 3the answer is 4, one per cell. - Negative targets. They compare like any other number.
- Stopping at the first match. A
returninside the loop would make this a "does it exist" function. Counting needs the loops to finish. - Counting rows. A row that contains the target several times must add several times, not once.
Testing
[[1, 2, 3], [4, 2, 6], [2, 8, 2]]withtarget = 2gives4.[[-1, 5], [5, -1]]withtarget = -1gives2.[[7, 8, 9]]withtarget = 4gives0.[[3, 3], [3, 3]]withtarget = 3gives4.
