Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance from All Buildings

med
30 min
Explore how to apply Tree Breadth-First Search to find the optimal location in a grid for a new house by minimizing the sum of shortest distances to all buildings. Understand movement constraints, obstacle handling, and shortest path calculations to solve this common coding interview problem.

Statement

Given an m x n integer grid grid, where each cell contains one of three values:

  • 00 represents empty land that can be freely traversed,

  • 11 represents a building that cannot be passed through,

  • 22 represents an obstacle that cannot be passed through.

You want to place a house on an empty land cell (00) such that the sum of shortest distances from the house to all buildings is minimized. Movement is restricted to four directions: up, down, left, and right.

Return the minimum total travel distance for such a placement. If no valid empty land cell can reach all buildings, return 1-1.

Note: The total travel distance is the sum of the shortest path distances from the chosen empty land cell to every building in the grid.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 11 \leq m, n 50\leq 50

  • grid[i][j] is either 00, 11, or 22

  • There will be at least 11 building in grid

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance from All Buildings

med
30 min
Explore how to apply Tree Breadth-First Search to find the optimal location in a grid for a new house by minimizing the sum of shortest distances to all buildings. Understand movement constraints, obstacle handling, and shortest path calculations to solve this common coding interview problem.

Statement

Given an m x n integer grid grid, where each cell contains one of three values:

  • 00 represents empty land that can be freely traversed,

  • 11 represents a building that cannot be passed through,

  • 22 represents an obstacle that cannot be passed through.

You want to place a house on an empty land cell (00) such that the sum of shortest distances from the house to all buildings is minimized. Movement is restricted to four directions: up, down, left, and right.

Return the minimum total travel distance for such a placement. If no valid empty land cell can reach all buildings, return 1-1.

Note: The total travel distance is the sum of the shortest path distances from the chosen empty land cell to every building in the grid.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 11 \leq m, n 50\leq 50

  • grid[i][j] is either 00, 11, or 22

  • There will be at least 11 building in grid

Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths