call(back)
Algorithms & Data StructureseasySign in to star problems

People and Cakes V

The same full grid of people (1) and cakes (2). The person at [row, col] walks to the cake nearest to them by Manhattan distance; that cake is unique. Return its [row, col].

cakeFor([[2, 1, 1], [1, 1, 2], [1, 1, 1]], 2, 1)  ->  [1, 2]
cakeFor([[2, 1, 1]], 0, 2)                        ->  [0, 0]

From [2, 1] the cake at [0, 0] is 3 away and the cake at [1, 2] is 2 away.

Hints

Hint 1

Manhattan distance is |dr| + |dc|; there is nothing to search, only cakes to compare.

Hint 2

One pass over the grid, remembering the best cake so far, is already optimal — every cell may hold a cake.

Topics

Asked at

Similar Problems6