FastPrepAvoiding the Obstacles (IMC Sydney 🦪)

Avoiding the Obstacles (IMC Sydney 🦪)

IMC Trading logoIMC Trading● HardINTERNFULLTIMENEW GRADOA

Problem statement

In this game, a player begins on a two-dimensional grid of size n × m. One cell of the grid is marked as the end and the player wants to reach this cell by moving up, down, left or right. However, some cells are occupied by obstacles. The goal of the player is to reach the end cell using a path that maximizes the minimum distance from any obstacle along that path.

The distance between any two points on the grid with coordinates (x1, y1) and (x2, y2) is calculated as |x1 - x2| + |y1 - y2|, where |a| is the absolute value of integer a.

Notes:

  • One can visit a cell with an obstacle if necessary, i.e. no other path exists.
  • A cell can be visited only once on a given path.
  • The input start and end positions may be occupied by obstacles, and may coincide. If they coincide, the path contains only that cell.

Function

findBestPath(n: int, m: int, startRow: int, startColumn: int, endRow: int, endColumn: int, obstacleRow: int[], obstacleColumn: int[]) → int

Complete the function findBestPath in the editor below.

findBestPath has the following parameters:

  1. int n: the number of rows in the grid
  2. int m: the number of columns in the grid
  3. int startRow: the row index of the starting position
  4. int startColumn: the column index of the starting position
  5. int endRow: the row index of the ending position
  6. int endColumn: the column index of the ending position
  7. int[] obstacleRow: the row index of the ith obstacle
  8. int[] obstacleColumn: the column index of the ith obstacle

Returns int: the largest possible minimum distance from an obstacle in any path from the starting point to the ending point

Examples

Example 1

n = 4m = 4startRow = 0startColumn = 0endRow = 3endColumn = 3obstacleRow = [0, 1]obstacleColumn = [3, 2]return = 2
Example 1 illustration

For the obstacles at (0, 3) and (1, 2), take the path (0, 0) → (1, 0) → (2, 0) → (3, 0) → (3, 1) → (3, 2) → (3, 3). The nearest-obstacle Manhattan distances along these seven cells are 3, 2, 3, 4, 3, 2, 3. The path’s minimum is 2. A threshold of 3 cannot connect the start to the end, so the maximum possible minimum distance is 2.

Example 2

n = 5m = 3startRow = 1startColumn = 1endRow = 4endColumn = 2obstacleRow = [0,2]obstacleColumn = [2,2]return = 2

The obstacles are at (0, 2) and (2, 2). The path (1, 1) → (1, 0) → (2, 0) → (3, 0) → (4, 0) → (4, 1) → (4, 2) has nearest-obstacle distances 2, 3, 2, 3, 4, 3, 2, so its minimum is 2. Both endpoints have nearest-obstacle distance 2, which is an upper bound for every path; this path attains it.

Example 3

n = 3m = 3startRow = 0startColumn = 1endRow = 2endColumn = 2obstacleRow = [1,1,1]obstacleColumn = [0,1,2]return = 0

Every path from the top row to the bottom row crosses the middle row, where all three cells contain obstacles. Such a cell has distance 0, and obstacle cells may be visited, so the maximum possible minimum distance is 0.

The formal endpoint is (2, 2); another diagram places E at (2, 1). Some photographs also print the final monster-column token 1 beside the coordinate annotation (1, 2). For this exercise, assume the final monster is at (1, 2), so this runnable example uses the annotated full row [0, 1, 2], consistent with the barrier diagram and stated output 0. The numeric discrepancy is preserved as a source transcription conflict.

Constraints

  • For this combined-source exercise, 1 <= n, m <= 400.
  • 0 <= startRow, endRow < n
  • 0 <= startColumn, endColumn < m
  • 1 <= obstacleRow.length = obstacleColumn.length <= n * m
  • 0 <= obstacleRow[i] < n and 0 <= obstacleColumn[i] < m.
  • Obstacle coordinates are distinct.
  • The start and end may be occupied by obstacles or coincide with each other.

More IMC Trading problems

See IMC Trading hiring insights
Saved
public int findBestPath(int n, int m, int startRow, int startColumn, int endRow, int endColumn, int[] obstacleRow, int[] obstacleColumn) {
    // write your code here
}
n4
m4
startRow0
startColumn0
endRow3
endColumn3
obstacleRow[0, 1]
obstacleColumn[3, 2]
expected2
Checking account…