本文共 998 字,大约阅读时间需要 3 分钟。
题目:(中等)
标签:广度优先搜索、堆
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N l o g N ) O(NlogN) O(NlogN) | O ( N ) O(N) O(N) | 948ms (100.00%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
import heapqclass Solution: def maximumMinimumPath(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) def _is_valid(x, y): return 0 <= x < m and 0 <= y < n def neighbors(x1, y1): return [(x2, y2) for (x2, y2) in [(x1 - 1, y1), (x1 + 1, y1), (x1, y1 - 1), (x1, y1 + 1)] if _is_valid(x2, y2)] ans = grid[0][0] visited = { (0, 0)} queue = [(-grid[0][0], 0, 0)] while queue: v1, i1, j1 = heapq.heappop(queue) ans = min(ans, -v1) if i1 == m - 1 and j1 == n - 1: return ans for i2, j2 in neighbors(i1, j1): if (i2, j2) not in visited: visited.add((i2, j2)) heapq.heappush(queue, (-grid[i2][j2], i2, j2)) return ans
转载地址:http://cxkcf.baihongyu.com/