We are tracking Elsa, an arctic fox, through a rectangular snowy field represented by a binary grid, field, where a 1 denotes snowprints and a 0 denotes no snowprints. We know that the fox crossed the field from left to right, so each column has exactly one 1.
Between two consecutive columns, the row of the 1 may remain the same, go up by one, or go down by one. Above the field (above the first row), there is an icy river. Return how close the fox got to the river, in terms of the number of rows between it and the river. Assume that field has at least one row and one column.
Example:
The field from Figure 6.
- Input:
[ [0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [1, 1, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1] ] - Output:
1 - Explanation: The fox was the closest to the river in column 2 (row index 1).
0
0
0
0
0
0
0
0
1
0
0
0
1
1
0
1
0
0
0
0
0
0
1
1
minDist=4
r=-1
c=-1
Step 1 / 8
Step 1:
Initialize minDist to max possible value (rows=4). Start scanning columns.
Focus: default
minDist=4r=-1c=-1