Day57力扣打卡
2023-12-13 03:47:55
打卡记录
最小体力消耗路径
Dijkstra
将Dijkstra算法从计算最短路径转化为计算路径最大差值。
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
n, m = len(heights), len(heights[0])
dist = [0] + [0x3f3f3f3f] * (n * m - 1)
vis = set()
q = [(0, 0, 0)]
while q:
d, x, y = heappop(q)
idx = x * m + y
if idx in vis:
continue
vis.add(idx)
for nx, ny in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if 0 <= nx < n and 0 <= ny < m:
max_diff = max(d, abs(heights[x][y] - heights[nx][ny]))
if max_diff < dist[nx * m + ny]:
dist[nx * m + ny] = max_diff
heappush(q, (max_diff, nx, ny))
return dist[-1]
二分 + BFS
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
n, m = len(heights), len(heights[0])
def check(t):
vis = set()
q = collections.deque()
q.append((0, 0))
vis.add(0)
while q:
x, y = q.popleft()
for nx, ny in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if 0 <= nx < n and 0 <= ny < m and nx * m + ny not in vis:
max_diff = abs(heights[x][y] - heights[nx][ny])
if max_diff <= t:
q.append((nx, ny))
vis.add(nx * m + ny)
return m * n - 1 in vis
l, r = 0, 10 ** 6 + 1
while l < r:
mid = (l + r) // 2
if check(mid):
r = mid
else:
l = mid + 1
return l
文章来源:https://blog.csdn.net/qq947467490/article/details/134921351
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!