惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
L
LangChain Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
IT之家
IT之家
A
About on SuperTechFans
H
Help Net Security

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree When Dijkstra Algorithm Should be Use? 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
1102. Path With Maximum Minimum Value
北叶青藤 · 2026-03-20 · via 博客园 - 北叶青藤

Given an m x n integer matrix grid, return the maximum score of a path starting at (0, 0) and ending at (m - 1, n - 1) moving in the 4 cardinal directions.

The score of a path is the minimum value in that path.

  • For example, the score of the path 8 → 4 → 5 → 9 is 4.

Example 1:

Input: grid = [[5,4,5],[1,2,6],[7,4,6]]
Output: 4
Explanation: The path with the maximum score is highlighted in yellow. 

Example 2:

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

Example 3:

Input: grid = [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]
Output: 3

dfs approach

def maximumMinimumPath(grid):
    R, C = len(grid), len(grid[0])
    max_min_val = 0
    visited = [[False for _ in range(C)] for _ in range(R)]

    def dfs(r, c, current_min):
        nonlocal max_min_val
        
        # Update the minimum value seen on THIS specific path
        current_min = min(current_min, grid[r][c])
        
        # Pruning: If our current path is already worse than the best 
        # path we've found so far, stop exploring it.
        if current_min <= max_min_val:
            return

        # If we reached the goal, update our global best
        if r == R - 1 and c == C - 1:
            max_min_val = max(max_min_val, current_min)
            return

        visited[r][c] = True
        
        # Explore neighbors
        for dr, dc in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
            nr, nc = r + dr, c + dc
            
            if 0 <= nr < R and 0 <= nc < C and not visited[nr][nc]:
                dfs(nr, nc, current_min)
                
        # Backtrack: Unmark visited so other paths can use this cell
        visited[r][c] = False

    dfs(0, 0, grid[0][0])
    return max_min_val

better approach

To solve the Path With Maximum Minimum Value problem (LeetCode 1102), the goal is to find a path from $(0, 0)$ to $(R-1, C-1)$ such that the minimum value encountered along the path is as large as possible.

The Logic: "Greedy" Max-Priority Queue

This is a classic variation of a shortest path problem, but instead of minimizing the sum, we are maximizing the bottleneck.

We use a Max-Priority Queue (Max-Heap) to always explore the cell with the highest value currently available to us. By greedily picking the highest value neighbor, the first time we reach the destination, the "minimum value seen so far" is guaranteed to be the maximum possible minimum for any path.

 1 import heapq
 2 
 3 def maximumMinimumPath(grid):
 4     R, C = len(grid), len(grid[0])
 5     
 6     # Max-heap stores (-value, row, col)
 7     # We start at (0, 0)
 8     max_heap = [(-grid[0][0], 0, 0)]
 9     
10     # Keep track of the minimum value encountered on our current best path
11     res = grid[0][0]
12     
13     visited = [[False for _ in range(C)] for _ in range(R)]
14     visited[0][0] = True
15     
16     while max_heap:
17         val, r, c = heapq.heappop(max_heap)
18         val = -val # Convert back to positive
19         
20         # The bottleneck of the path is the minimum value we've seen
21         res = min(res, val)
22         
23         # If we reached the goal, this is the maximum possible bottleneck
24         if r == R - 1 and c == C - 1:
25             return res
26         
27         # Explore 4-directional neighbors
28         for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
29             nr, nc = r + dr, c + dc
30             
31             if 0 <= nr < R and 0 <= nc < C and not visited[nr][nc]:
32                 visited[nr][nc] = True
33                 heapq.heappush(max_heap, (-grid[nr][nc], nr, nc))
34                 
35     return res