Problem · Matrix

Closest DashMart

Learn this problem
â—Ź MediumDoorDash logoDoorDashFULLTIMEOAPHONE SCREEN

Problem statement

A DashMart is a warehouse that houses items found in convenience stores, grocery stores, and restaurants. A city is represented as a rectangular grid containing open roads, blocked roads, and DashMarts.

Each grid cell contains one of the following values:

  • ' ' represents an open road.
  • 'X' represents a blocked road that cannot be entered or crossed during travel.
  • 'D' represents a DashMart.

For each location in locations, return the shortest number of orthogonal steps needed to reach any DashMart. A step may move up, down, left, or right.

Apply these rules to every query:

  • A location outside the grid has distance -1.
  • A location on a DashMart has distance 0.
  • A location on an open road may travel only through open roads and DashMarts.
  • A location on a blocked road may take one first step to an orthogonally adjacent open road or DashMart. After leaving that starting cell, it may travel only through open roads and DashMarts. No route may enter any blocked road, including the starting cell again.
  • If no DashMart is reachable under these rules, the distance is -1.

Locations use [row, col] coordinates. Return the distances in the same order as the queries.

Function

closestDashMart(city: char[][], locations: int[][]) → int[]

Examples

Example 1

city = [
  ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'],
  ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'],
  [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '],
  [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '],
  [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'],
  [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X']
]locations = [[200, 200], [1, 4], [0, 3], [5, 8], [1, 8], [5, 5]]return = [-1, 2, 0, -1, 6, 9]

[200, 200] is outside the grid, so its result is -1. From [1, 4], the closest DashMart is two steps away at [0, 3]. The location [0, 3] is already a DashMart, so its distance is 0.

Both [5, 8] and [1, 8] are blocked cells. The cell [5, 8] has no adjacent traversable cell from which a DashMart can be reached, so its result is -1. From [1, 8], leave the blocked starting cell through [1, 7], then follow open roads to [0, 3] in a total of 6 steps.

From [5, 5], a shortest open-road route reaches [0, 3] in 9 steps.

Constraints

  • city.length >= 1.
  • Every row of city has the same positive length.
  • Every cell of city is ' ', 'X', or 'D'.
  • locations.length >= 0.
  • Every element of locations contains exactly two integers, [row, col].
  • A query coordinate may be outside the city grid.

More DoorDash problems

drafts saved locally
public int[] closestDashMart(char[][] city, int[][] locations) {
  // write your code here
}
city[ ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'], ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'], [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '], [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'], [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'] ]
locations[[200, 200], [1, 4], [0, 3], [5, 8], [1, 8], [5, 5]]
expected[-1, 2, 0, -1, 6, 9]
checking account