-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathWithMinimumEffort.py
More file actions
38 lines (34 loc) · 1.15 KB
/
pathWithMinimumEffort.py
File metadata and controls
38 lines (34 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/path-with-minimum-effort/
# Author: Miao Zhang
# Date: 2021-05-24
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
m = len(heights)
n = len(heights[0])
dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]
def bfs(cost: int) -> bool:
q = collections.deque()
seen = [0] * (m * n)
q.append([0, 0])
seen[0] = 1
while q:
i, j = q.popleft()
if i == m - 1 and j == n - 1: return True
for d in dirs:
x = i + d[0]
y = j + d[1]
if x < 0 or x >= m or y < 0 or y >= n or seen[x * n + y]: continue
if abs(heights[x][y] - heights[i][j]) > cost: continue
seen[x * n + y] = 1
q.append([x, y])
return False
l, r = 0, 10 ** 6 + 1
while l < r:
mid = l + (r - l) // 2
if bfs(mid):
r = mid
else:
l = mid + 1
return l