KatInterview

Climbing Routes From the Corner: Batch Lookup of Shortest Paths

Trees & GraphsAuto-graded · 22 test cases
HardAuto-gradedTrees & Graphsbfsgridshortest-path

Last updated

Run one BFS from the top-left cell of a grid with distinct values, using strictly increasing moves, then answer many target queries by rebuilding paths from stored parent links.

Climbing Routes From the Corner

You are given a rectangular grid of integers and a list of query values. For each query value, find a shortest route that starts at the top-left cell (0, 0) and ends at the cell holding that value, where every step goes to a strictly larger value.

From any cell you may step to its up, right, down, or left neighbor, as long as that neighbor lies inside the grid and holds a strictly greater value than the current cell.

Implement shortest_climbs(grid, queries). It returns one list per query, in the same order as queries. Each list contains the values along the chosen route, from the starting cell to the target cell, inclusive.

Rules

  • The length of a route is the number of steps (edges). A route that consists only of the start cell has zero steps and is the list [grid[0][0]].
  • If the query value does not appear in the grid, or it appears but cannot be reached from the top-left cell by increasing moves, the answer for that query is an empty list [].
  • Several shortest routes may exist. Choose the one defined by this procedure: run a breadth-first search from (0, 0) using a FIFO queue. When expanding a cell, examine its neighbors in the order up, right, down, left. Each cell is assigned to the first cell that discovers it, and that assignment is never changed. The route to a target is the chain of discoverers leading back to the start.
  • The grid never changes between queries, so do the traversal work once up front. Answering an individual query should only need a lookup plus building the output route, not a fresh search of the grid.

Constraints

  • The grid has between 1 and 100 rows and between 1 and 100 columns, and it is rectangular.
  • There are at most 1,000 queries.
  • All grid values are distinct signed 32-bit integers, so each value identifies at most one cell.
  • Query values are signed 32-bit integers and may repeat; each occurrence gets its own answer list.

Examples

Example 1

Input: shortest_climbs([[1, 2, 3], [4, 5, 8], [7, 9, 6]], [5, 6, 1])

Output: [[1, 2, 5], [], [1]]

Example 2

Input: shortest_climbs([[4, 3], [5, 6]], [6, 3, 9])

Output: [[4, 5, 6], [], []]

Hints

Hint 1

Only the destination changes between queries. Think about what can be computed once from the fixed start cell and shared by every query.

Hint 2

A breadth-first search on unit-cost moves finds shortest step counts. If you record, for each cell, which cell first discovered it, those links describe a route back to the start for every reachable cell.

Hint 3

Map each value to its cell with a dictionary. To answer a query, check that the cell was reached, follow the discoverer links back to the start, and reverse the collected values. Storing a full route per cell would take far more memory than one link per cell.