Find Path with Highest Bottleneck Value in Grid
Heaps, Sorting & SearchAuto-graded · 21 test casesLast 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 <= 5000 <= 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?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a bottleneck shortest path problem where we want to maximize the minimum value encountered. The key insight is to adapt Dijkstra's algorithm by changing how we combine path values and prioritize exploration.
Instead of summing edge weights, we take the minimum of the current path score and the next cell's value. Instead of exploring the lowest-cost frontier first, we explore the highest-score frontier first using a max-heap (simulated with negated values in Python's min-heap).
We maintain a best array tracking the highest bottleneck score found so far for reaching each cell. Starting from (0, 0) with score grid[0][0], we repeatedly:
- Pop the cell with the highest score from the priority queue
- If this cell is the destination, return its score
- For each neighbor, compute the new path score as
min(current_score, neighbor_value) - If this improves the neighbor's best score, update it and push to the heap
The algorithm works because the bottleneck operation is monotone non-increasing: extending a path can never increase its minimum value. When we pop a cell with score s and s is the maximum in the queue, no other path can reach that cell with a higher score. This is the same correctness argument as Dijkstra's algorithm, adapted for the min operation instead of addition.
The lazy deletion check if score != best[r][c] skips outdated heap entries when a cell has been reached with a better score since the entry was pushed.
Complexity
Time: O(nm log(nm)) where n and m are the grid dimensions. We have nm cells and approximately 4nm edges. Each cell is pushed to the heap at most once per distinct improvement, and each heap operation costs O(log(nm)).
Space: O(nm) for the best array and priority queue.
Solution
import heapq
from typing import List
def highest_bottleneck_path(grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
best = [[-1] * m for _ in range(n)]
best[0][0] = grid[0][0]
heap = [(-grid[0][0], 0, 0)]
directions = ((1, 0), (-1, 0), (0, 1), (0, -1))
while heap:
neg_score, r, c = heapq.heappop(heap)
score = -neg_score
if score != best[r][c]:
continue
if r == n - 1 and c == m - 1:
return score
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < m:
next_score = min(score, grid[nr][nc])
if next_score > best[nr][nc]:
best[nr][nc] = next_score
heapq.heappush(heap, (-next_score, nr, nc))
return best[n - 1][m - 1]