-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path2290-minimum-obstacle-removal-to-reach-corner.py
35 lines (29 loc) · 1.23 KB
/
2290-minimum-obstacle-removal-to-reach-corner.py
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
# time complexity: O(m*n*log(mn))
# space complexity: O(m*n)
from heapq import heappop, heappush
from typing import List
class Solution:
def minimumObstacles(self, grid: List[List[int]]) -> int:
ROW = len(grid)
COL = len(grid[0])
Dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)]
obsticleGird = [[float('inf')] * COL for _ in range(ROW)]
obsticleGird[0][0] = grid[0][0]
pq = [(grid[0][0], 0, 0)]
while pq:
currObsticle, currR, currC = heappop(pq)
if currR == ROW - 1 and currC == COL - 1:
return currObsticle
for dR, dC in Dirs:
nextR = currR + dR
nextC = currC + dC
if 0 <= nextR < ROW and 0 <= nextC < COL:
nextObsticle = currObsticle + grid[nextR][nextC]
if nextObsticle < obsticleGird[nextR][nextC]:
obsticleGird[nextR][nextC] = nextObsticle
heappush(pq, (nextObsticle, nextR, nextC))
return obsticleGird[ROW - 1][COL-1]
grid = [[0, 1, 1], [1, 1, 0], [1, 1, 0]]
print(Solution().minimumObstacles(grid))
grid = [[0, 1, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 1, 0]]
print(Solution().minimumObstacles(grid))