Problem overview
The method findInGrid receives grid, a list of rows of integers, and an integer target. It returns the position of the first cell equal to target as a list of two integers: the row and the column. If no cell matches, it returns [-1, -1].
"First" means reading order: the top row from left to right, then the next row, and so on. Rows are numbered from 0 at the top and columns from 0 at the left.
In a grid with rows [1, 2, 3], [4, 5, 6] and [7, 8, 9], the value 6 is in row 1 and column 2, so the answer is [1, 2]. In [[5, 1], [1, 5]] the value 1 appears twice, and reading row by row the one at row 0, column 1 comes first: [0, 1]. In a grid of only 4s, searching for 3 gives [-1, -1].
Approach
Counting only needs the values, but this problem needs positions, so the loops must run over indexes, not over the values themselves. An outer loop goes through row numbers and an inner loop through the column numbers of that row. The inner loop finishes a whole row before the next row starts, which is the reading order the problem asks for.
Because only the first match matters, the function can answer the moment it finds one. Everything after that cell is irrelevant. If both loops finish with no match, the answer is the "not found" pair.
Step by step
- Loop
rowoverrange(len(grid)).len(grid)is the number of rows, because the grid is a list of rows. - Inside it, loop
coloverrange(len(grid[row])).grid[row]is one row, so its length is the number of columns. - If
grid[row][col] == target, return[row, col]immediately.returnleaves the whole function, so both loops stop at the first match. Abreakwould only end the inner loop. - Return
[-1, -1]after both loops. The function gets there only when no cell matched, and -1 is never a real row or column number.
The solution in Python
class Solution:
def findInGrid(self, grid: List[List[int]], target: int) -> List[int]:
for row in range(len(grid)):
for col in range(len(grid[row])):
if grid[row][col] == target:
return [row, col]
return [-1, -1]
Python details:
- Row index first.
grid[row][col]picks rowrowand then the value at positioncolinside it. The answer[row, col]uses the same order. range(n)counts from 0 ton - 1, which matches the 0-based numbering of rows and columns.- Returning a new list. Each
returnbuilds a fresh list, so the caller can change it without affecting anything else.
Edge cases and common mistakes
- Using
breakinstead ofreturn. It ends only the inner loop, so the outer loop moves on to the next row and keeps searching. The function then ends with the wrong answer, a later match or[-1, -1], even though a match was found. - Swapping row and column in the answer. For 6 in the first example,
[2, 1]would be a different cell. - Several matches. Only the first in reading order is returned: for
[[5, 1], [1, 5]]and target 1, the answer is[0, 1], not[1, 0]. - Not found. For a grid with no match, both loops end and the function returns
[-1, -1]. - Target of 0 or a negative target. These are ordinary values. The not-found marker
[-1, -1]is a list of two numbers, so it can never be confused with a real position. - Returning inside the wrong place. Putting
return [-1, -1]inside the loops would give up after the first cell.
Testing
- The grid
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]with target6gives[1, 2]. [[5, 1], [1, 5]]with target1gives[0, 1].[[4, 4], [4, 4]]with target3gives[-1, -1].
