Return the k nearest restaurants to a starting cell in a grid with walls.
You are given a 2D grid representing a map. Each cell is ' ' (empty, traversable), '-' (a wall that cannot be traversed) or 'A' to 'Z' (a restaurant). You are also given a starting position (row, col) and an integer k. Return the top k nearest restaurants from the starting position based on the minimum number of steps required to reach them. Movement is in 4 directions (up, down, left, right).
Output: a dictionary/map of at most k restaurants with their minimum step counts; if fewer than k are reachable, return all reachable ones.
Constraints: grid size m x n, 1 <= m, n, k >= 1, the starting position is within bounds, restaurants are labeled 'A'-'Z'.
Signature: nearest_restaurants(grid: List[List[str]], start_row: int, start_col: int, k: int) -> Dict[str, int].
Format · Live coding on HackerRank after a background discussion.
Community answers
Related questions
Reach the end of a grid with direction cells and walls in a given codebase with failing tests.Detect a cycle in a linked list and return the starting node.Related courses


