KatInterview

Find Path with Highest Bottleneck Value in Grid

Heaps, Sorting & SearchAuto-graded · 21 test cases
MediumAuto-gradedHeaps, Sorting & Searchgraph-traversaldijkstrabinary-search

Last updated

Tests combining priority-queue graph traversal with bottleneck optimization to find the path whose minimum cell value is maximized.

You are given an n x m grid where each cell contains a non-negative integer representing its value. Your task is to find a path from the top-left cell (0, 0) to the bottom-right cell (n-1, m-1) that maximizes the minimum value encountered along the way.

You may move between adjacent cells in four directions: up, down, left, or right. A path's score is defined as the smallest value among all cells visited in that path (including start and end). Return the maximum possible score among all valid paths.

Constraints

  • 1 <= n, m <= 500
  • 0 <= grid[i][j] <= 1,000,000
  • You must move from (0, 0) to (n-1, m-1) using only orthogonal (non-diagonal) moves
  • You may visit the same cell multiple times, though optimal paths will not need to

Examples

Example 1

Input: highest_bottleneck_path([[5, 1], [4, 5]])

Output: 4

Example 2

Input: highest_bottleneck_path([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Output: 1

Hints

Hint 1

The score of any path is limited by both the starting and ending cell values. What does this tell you about the upper bound?

Hint 2

Consider binary search: if you can reach the destination using only cells with value >= K, can you also do it for any smaller threshold?

Hint 3

Think about Dijkstra's algorithm, but instead of minimizing distance sums, you want to maximize the minimum value along the path. How should you modify the priority and combination logic?