Minimum Time Takes to Reach Destination Without Drowning — LeetCode 2814 Python Solution
HardLeetCode PremiumBreadth-First SearchArrayMatrix
- Problem
- #2814
- Pattern
- Matrix and Grid
- Reading time
- 9 min
- Source
- leetcode.com
The problem
You are given an n * m 0-indexed grid of string land. Right now, you are standing at the cell that contains "S", and you want to get to the cell containing "D".
Example
- Input
- land = [["D",".","*"],[".",".","."],[".","S","."]]
- Output
- 3
- Explanation
- The picture below shows the simulation of the land second by second. The blue cells are flooded, and the gray cells are stone.
Python solution
Python
class Solution:
def minimumSeconds(self, land: List[List[str]]) -> int:
m, n = len(land), len(land[0])
vis = [[False] * n for _ in range(m)]
g = [[inf] * n for _ in range(m)]
q = deque()
si = sj = 0
for i, row in enumerate(land):
for j, c in enumerate(row):
match c:
case "*":
q.append((i, j))
case "S":
si, sj = i, j
dirs = (-1, 0, 1, 0, -1)
t = 0
while q:
for _ in range(len(q)):
i, j = q.popleft()
g[i][j] = t
for a, b in pairwise(dirs):
x, y = i + a, j + b
if (
0 <= x < m
and 0 <= y < n
and not vis[x][y]
and land[x][y] in ".S"
):
vis[x][y] = True
q.append((x, y))
t += 1
t = 0
q = deque([(si, sj)])
vis = [[False] * n for _ in range(m)]
vis[si][sj] = True
while q:
for _ in range(len(q)):
i, j = q.popleft()
if land[i][j] == "D":
return t
for a, b in pairwise(dirs):
x, y = i + a, j + b
if (
0 <= x < m
and 0 <= y < n
and g[x][y] > t + 1
and not vis[x][y]
and land[x][y] in ".D"
):
vis[x][y] = True
q.append((x, y))
t += 1
return -1Complexity
| Measure | Complexity |
|---|---|
| Time | O(m \times n) |
| Space | O(m \times n) auxiliary |
Pattern: Matrix and Grid
Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.
The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning?
- LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning?
- The Python solution on this page runs in O(m \times n).
- What is the space complexity of LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning?
- The Python solution on this page uses O(m \times n) auxiliary space.
- What topics does LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning cover?
- LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning is tagged Breadth-First Search, Array and Matrix on LeetCode.
- Is LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning a premium problem?
- Yes. LeetCode 2814. Minimum Time Takes to Reach Destination Without Drowning is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.