Approach
Breadth-first search
For Last Day Where You Can Still Cross, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 47 lines of Python from the credited upstream file 1970.py.
- The implementation visibly relies on sequence storage, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def latestDayToCross(self, row: int, col: int, cells: list[list[int]]) -> int:3 DIRS = ((0, 1), (1, 0), (0, -1), (-1, 0))4 5 def canWalk(day: int) -> bool:6 matrix = [[0] * col for _ in range(row)]7 for i in range(day):8 x, y = cells[i]9 matrix[x - 1][y - 1] = 110 11 q = collections.deque()12 13 for j in range(col):14 if matrix[0][j] == 0:15 q.append((0, j))16 matrix[0][j] = 117 18 while q:19 i, j = q.popleft()20 for dx, dy in DIRS:21 x = i + dx22 y = j + dy23 if x < 0 or x == row or y < 0 or y == col:24 continue25 if matrix[x][y] == 1:26 continue27 if x == row - 1:28 return True29 q.append((x, y))30 matrix[x][y] = 131 32 return False33 34 ans = 035 l = 136 r = len(cells) - 137 38 while l <= r:39 m = (l + r) 240 if canWalk(m):41 ans = m42 l = m + 143 else:44 r = m - 145 46 return ans47