Doordash Onsite
Anonymous User
16906

// # A DashMart is a warehouse run by DoorDash that houses items found in
// # convenience stores, grocery stores, and restaurants. We have a city with open
// # roads, blocked-off roads, and DashMarts.
// #
// # City planners want you to identify how far a location is from its closest
// # DashMart.
// #
// # You can only travel over open roads (up, down, left, right).
// #
// # Locations are given in [row, col] format.
// #
// # Example:
// #
// # [
// # # 0 1 2 3 4 5 6 7 8
// # ['X', ' ', ' ', 'D', ' ', ' ', 'X', ' ', 'X'], # 0
// # ['X', ' ', 'X', 'X', ' ', ' ', ' ', ' ', 'X'], # 1
// # [' ', ' ', ' ', 'D', 'X', 'X', ' ', 'X', ' '], # 2
// # [' ', ' ', ' ', 'D', ' ', 'X', ' ', ' ', ' '], # 3
// # [' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X'], # 4
// # [' ', ' ', ' ', ' ', 'X', ' ', ' ', 'X', 'X'] # 5
// # ]
// #
// # ' ' represents an open road that you can travel over in any direction (up, down, left, or right).
// # 'X' represents an blocked road that you cannot travel through.
// # 'D' represents a DashMart.
// #
// # # list of pairs [row, col]
// # locations = [
// # [2, 2],
// # [4, 0],
// # [0, 4],
// # [2, 6],
// # ]
// #
// # answer = [1, 4, 1, 5]
// #
// # Implement Function:
// # - get_closest_dashmart(city, locations)
// #
// # Provided:
// # - city: List[str]
// # - locations: List[List[int]]
// #
// # Return:
// # - answer: List[int]

I solved it by triggering a BFS from all the D's and storing the result. I did stop the BFS if we could see that the BFS will not yeild a better result in terms of distance than already stored there. Also handled the X's by not considering them as the neighbour. I think there was a better answer for this, care to help?

Comments (15)