Skip to content

Commit 185d259

Browse files
committed
Sync LeetCode submission Runtime - 152 ms (28.07%), Memory - 18.2 MB (90.46%)
1 parent 77ac1db commit 185d259

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<p>There is a dungeon with <code>n x m</code> rooms arranged as a grid.</p>
2+
3+
<p>You are given a 2D array <code>moveTime</code> of size <code>n x m</code>, where <code>moveTime[i][j]</code> represents the <strong>minimum</strong> time in seconds when you can <strong>start moving</strong> to that room. You start from the room <code>(0, 0)</code> at time <code>t = 0</code> and can move to an <strong>adjacent</strong> room. Moving between adjacent rooms takes <em>exactly</em> one second.</p>
4+
5+
<p>Return the <strong>minimum</strong> time to reach the room <code>(n - 1, m - 1)</code>.</p>
6+
7+
<p>Two rooms are <strong>adjacent</strong> if they share a common wall, either <em>horizontally</em> or <em>vertically</em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">moveTime = [[0,4],[4,4]]</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">6</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p>The minimum time required is 6 seconds.</p>
20+
21+
<ul>
22+
<li>At time <code>t == 4</code>, move from room <code>(0, 0)</code> to room <code>(1, 0)</code> in one second.</li>
23+
<li>At time <code>t == 5</code>, move from room <code>(1, 0)</code> to room <code>(1, 1)</code> in one second.</li>
24+
</ul>
25+
</div>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>Input:</strong> <span class="example-io">moveTime = [[0,0,0],[0,0,0]]</span></p>
31+
32+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<p>The minimum time required is 3 seconds.</p>
37+
38+
<ul>
39+
<li>At time <code>t == 0</code>, move from room <code>(0, 0)</code> to room <code>(1, 0)</code> in one second.</li>
40+
<li>At time <code>t == 1</code>, move from room <code>(1, 0)</code> to room <code>(1, 1)</code> in one second.</li>
41+
<li>At time <code>t == 2</code>, move from room <code>(1, 1)</code> to room <code>(1, 2)</code> in one second.</li>
42+
</ul>
43+
</div>
44+
45+
<p><strong class="example">Example 3:</strong></p>
46+
47+
<div class="example-block">
48+
<p><strong>Input:</strong> <span class="example-io">moveTime = [[0,1],[1,2]]</span></p>
49+
50+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
51+
</div>
52+
53+
<p>&nbsp;</p>
54+
<p><strong>Constraints:</strong></p>
55+
56+
<ul>
57+
<li><code>2 &lt;= n == moveTime.length &lt;= 50</code></li>
58+
<li><code>2 &lt;= m == moveTime[i].length &lt;= 50</code></li>
59+
<li><code>0 &lt;= moveTime[i][j] &lt;= 10<sup>9</sup></code></li>
60+
</ul>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Approach: Shortest Path + Dijkstra
2+
3+
# n = no. of rows, m = no. of cols
4+
# Time: O(nm log(nm))
5+
# Space: O(nm)
6+
7+
import heapq
8+
9+
class State:
10+
def __init__(self, x, y, dist):
11+
self.x = x
12+
self.y = y
13+
self.dist = dist
14+
15+
def __lt__(self, other):
16+
return self.dist < other.dist
17+
18+
19+
class Solution:
20+
def minTimeToReach(self, moveTime: List[List[int]]) -> int:
21+
n, m = len(moveTime), len(moveTime[0])
22+
d = [[float('inf')] * m for _ in range(n)]
23+
v = [[0] * m for _ in range(n)]
24+
25+
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
26+
27+
d[0][0] = 0
28+
q = []
29+
heapq.heappush(q, State(0, 0, 0))
30+
31+
while q:
32+
s = heapq.heappop(q)
33+
if v[s.x][s.y]:
34+
continue
35+
v[s.x][s.y] = 1
36+
37+
for dx, dy in dirs:
38+
nx, ny = s.x + dx, s.y + dy
39+
if not (0 <= nx < n and 0 <= ny < m):
40+
continue
41+
dist = max(d[s.x][s.y], moveTime[nx][ny]) + 1
42+
if d[nx][ny] > dist:
43+
d[nx][ny] = dist
44+
heapq.heappush(q, State(nx, ny, dist))
45+
46+
return d[n - 1][m - 1]
47+

0 commit comments

Comments
 (0)