Quick Overview

Minimize a delivery grid’s worst nearest-center distance by adding at most one center. Use Chebyshev geometry and handle grids with no centers, remaining demand cells, and the zero-inconvenience boundary.

Place one delivery center to minimize the worst Chebyshev distance

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

A binary grid contains delivery centers at cells with value 1 and unserved locations at cells with value 0. The distance between `(r1, c1)` and `(r2, c2)` is the Chebyshev distance `max(abs(r1-r2), abs(c1-c2))`. The grid's inconvenience is the largest, over all zero cells, of the distance to the nearest center. You may convert at most one zero cell into a center. Return the smallest possible inconvenience afterward. ### Function Signature ```python def minimum_inconvenience(grid: list[list[int]]) -> int: ``` ### Rules - Every existing center remains in place. - A newly added center is no longer a zero cell and is excluded from the remaining demand locations. - If no zero cells remain, inconvenience is 0. - A grid may initially have no centers. In that case, place a center so that every remaining zero cell has a finite nearest-center distance. - Return the minimum inconvenience only. No placement tie-break is needed. ### Constraints These bounds and empty-set conventions define this practice version. - The grid has between 1 and 300 rows and between 1 and 300 columns. - Every row has the same length and every cell is 0 or 1. - Distances are nonnegative integers at most 299 after a center exists. ### Examples ```text Input: grid = [[0, 0, 0]] Output: 1 ``` Place a center in the middle. Each remaining zero cell is at distance 1. ```text Input: grid = [[1, 0], [0, 0]] Output: 1 ``` The existing center is within distance 1 of every zero cell, including the diagonal cell. One conversion cannot remove all zero cells, so inconvenience cannot become 0. ```text Input: grid = [[0]] Output: 0 ``` Converting the only cell leaves no zero cells.

Overview: Minimize a delivery grid’s worst nearest-center distance by adding at most one center. Use Chebyshev geometry and handle grids with no centers, remaining demand cells, and the zero-inconvenience boundary.

Read the full Amazon Software Engineer interview experience this question came from

A binary grid contains delivery centers at cells with value 1 and unserved locations at cells with value 0. The distance between `(r1, c1)` and `(r2, c2)` is the Chebyshev distance `max(abs(r1-r2), abs(c1-c2))`. The grid's inconvenience is the largest, over all zero cells, of the distance to the nearest center. You may convert at most one zero cell into a center. Return the smallest possible inconvenience afterward. ### Function Signature ```python def minimum_inconvenience(grid: list[list[int]]) -> int: ``` ### Rules - Every existing center remains in place. - A newly added center is no longer a zero cell and is excluded from the remaining demand locations. - If no zero cells remain, inconvenience is 0. - A grid may initially have no centers. In that case, place a center so that every remaining zero cell has a finite nearest-center distance. - Return the minimum inconvenience only. No placement tie-break is needed. ### Constraints These bounds and empty-set conventions define this practice version. - The grid has between 1 and 300 rows and between 1 and 300 columns. - Every row has the same length and every cell is 0 or 1. - Distances are nonnegative integers at most 299 after a center exists. ### Examples ```text Input: grid = [[0, 0, 0]] Output: 1 ``` Place a center in the middle. Each remaining zero cell is at distance 1. ```text Input: grid = [[1, 0], [0, 0]] Output: 1 ``` The existing center is within distance 1 of every zero cell, including the diagonal cell. One conversion cannot remove all zero cells, so inconvenience cannot become 0. ```text Input: grid = [[0]] Output: 0 ``` Converting the only cell leaves no zero cells.

Constraints

  • The grid has between 1 and 300 rows and between 1 and 300 columns.
  • Every row has the same length and every cell is 0 or 1.
  • Distances are nonnegative integers at most 299 after a center exists.

Examples

Input: ([[0, 0, 0]],)

Expected Output: 1

Explanation: A midpoint center serves both ends of an empty row.

Input: ([[1, 0], [0, 0]],)

Expected Output: 1

Explanation: Diagonal distance is one, and one conversion cannot cover three zero cells at radius zero.

Hints

  1. At a fixed proposed radius, focus on cells the existing centers do not already serve.
  2. A Chebyshev distance bound gives an independent permitted range for each coordinate.

Place one delivery center to minimize the worst Chebyshev distance

mediumAmazonSoftware Engineer

A binary grid contains delivery centers at cells with value 1 and unserved locations at cells with value 0. The distance between (r1, c1) and (r2, c2) is the Chebyshev distance max(abs(r1-r2), abs(c1-c2)).

The grid's inconvenience is the largest, over all zero cells, of the distance to the nearest center. You may convert at most one zero cell into a center. Return the smallest possible inconvenience afterward.

Function Signature

def minimum_inconvenience(grid: list[list[int]]) -> int:

Rules

  • Every existing center remains in place.
  • A newly added center is no longer a zero cell and is excluded from the remaining demand locations.
  • If no zero cells remain, inconvenience is 0.
  • A grid may initially have no centers. In that case, place a center so that every remaining zero cell has a finite nearest-center distance.
  • Return the minimum inconvenience only. No placement tie-break is needed.

Constraints

These bounds and empty-set conventions define this practice version.

  • The grid has between 1 and 300 rows and between 1 and 300 columns.
  • Every row has the same length and every cell is 0 or 1.
  • Distances are nonnegative integers at most 299 after a center exists.

Examples

Input: grid = [[0, 0, 0]]
Output: 1

Place a center in the middle. Each remaining zero cell is at distance 1.

Input: grid = [[1, 0], [0, 0]]
Output: 1

The existing center is within distance 1 of every zero cell, including the diagonal cell. One conversion cannot remove all zero cells, so inconvenience cannot become 0.

Input: grid = [[0]]
Output: 0

Converting the only cell leaves no zero cells.

Examples

Example 1
Input
([[0, 0, 0]],)
Output
1
Notes
A midpoint center serves both ends of an empty row.
Example 2
Input
([[1, 0], [0, 0]],)
Output
1
Notes
Diagonal distance is one, and one conversion cannot cover three zero cells at radius zero.

Constraints

  • •The grid has between 1 and 300 rows and between 1 and 300 columns.
  • •Every row has the same length and every cell is 0 or 1.
  • •Distances are nonnegative integers at most 299 after a center exists.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def minimum_inconvenience(grid: list[list[int]]) -> int:
Sample call
minimum_inconvenience([[0, 0, 0]])
Language
Case 1
Input
([[0, 0, 0]],)
Expected
1
Case 2
Input
([[1, 0], [0, 0]],)
Expected
1

Browse More Questions

Show the approach

Approach

Compute each cell's distance to its nearest existing center using multi-source breadth-first search with eight neighbors. Every eight-neighbor step changes either coordinate by at most one. A shortest route can move diagonally until one coordinate matches and then finish along the other, so the resulting distance is exactly the Chebyshev distance. With no centers, the distances stay above every candidate radius.

For a proposed radius t, collect cells whose current distance exceeds t. All such cells need to be within t of the new center. Each requires the new center's row and column to lie in an inclusive range. Intersect those ranges with the grid bounds. If no cell is far, no new center is needed. Otherwise a nonempty intersection gives a valid placement, since an existing center inside it would already serve every far cell. If the center is placed on a far cell, it is no longer demand and the range condition still holds.

Increasing t never harms feasibility. Binary search the first feasible radius from zero through the grid's maximum dimension minus one. That upper bound works with one center anywhere in the grid, including when none existed initially.

Time complexity:
O(H × W × log(max(H, W) + 1))
Space complexity:
O(H × W)