Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2814: Minimum Time Takes to Reach Destination Without Drowning

In this guide, we solve Leetcode #2814 Minimum Time Takes to Reach Destination Without Drowning in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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".

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: Breadth-First Search, Array, Matrix

Intuition

We need level-by-level exploration or shortest steps, which is ideal for BFS.

A queue naturally models the frontier of the search.

Approach

Push initial nodes into a queue and expand in layers.

Track visited nodes to prevent cycles.

Steps:

  • Initialize queue with start nodes.
  • Process level by level.
  • Track visited nodes.

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. Picture (0) shows the initial state and picture (3) shows the final state when we reach destination. As you see, it takes us 3 second to reach destination and the answer would be 3. It can be shown that 3 is the minimum time needed to reach from S to D.

Python Solution

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 -1

Complexity

The time complexity is O(m×n)O(m \times n)O(m×n) and the space complexity is O(m×n)O(m \times n)O(m×n). The space complexity is O(m×n)O(m \times n)O(m×n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy