Problem · Matrix

Place K Buildings to Minimize Maximum Grid Distance

Learn this problem
HardOracle logoOracleFULLTIMEONSITE INTERVIEW

Problem statement

Consider an empty grid with rows rows and columns columns. Place exactly k buildings in distinct cells.

The distance from a cell to its closest building is the minimum Manhattan distance to any building. Return the smallest possible value of the maximum such distance over every grid cell.

Function

minimumMaximumDistance(rows: int, columns: int, k: int) → int

Examples

Example 1

rows = 3columns = 3k = 1return = 2

Placing the building in the center leaves every corner at Manhattan distance 2.

Example 2

rows = 2columns = 3k = 2return = 1

Buildings in the middle column make every cell at most one step from a building.

Constraints

  • 1 <= rows, columns <= 8
  • 1 <= k <= rows * columns
  • Buildings occupy distinct cells.

More Oracle problems

drafts saved locally
public int minimumMaximumDistance(int rows, int columns, int k) {
    // Write your code here.
}
rows3
columns3
k1
expected2
checking account