Skip to content

Commit c4e60f9

Browse files
committed
Sync LeetCode submission Runtime - 2683 ms (33.90%), Memory - 93.3 MB (54.51%)
1 parent 185d259 commit c4e60f9

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 <strong>adjacent</strong> rooms takes one second for one move and two seconds for the next, <strong>alternating</strong> between the two.</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> 7</p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p>The minimum time required is 7 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 two seconds.</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,0,0]]</span></p>
31+
32+
<p><strong>Output:</strong> 6</p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<p>The minimum time required is 6 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 two seconds.</li>
41+
<li>At time <code>t == 3</code>, move from room <code>(1, 1)</code> to room <code>(1, 2)</code> in one second.</li>
42+
<li>At time <code>t == 4</code>, move from room <code>(1, 2)</code> to room <code>(1, 3)</code> in two seconds.</li>
43+
</ul>
44+
</div>
45+
46+
<p><strong class="example">Example 3:</strong></p>
47+
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">moveTime = [[0,1],[1,2]]</span></p>
50+
51+
<p><strong>Output:</strong> 4</p>
52+
</div>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>2 &lt;= n == moveTime.length &lt;= 750</code></li>
59+
<li><code>2 &lt;= m == moveTime[i].length &lt;= 750</code></li>
60+
<li><code>0 &lt;= moveTime[i][j] &lt;= 10<sup>9</sup></code></li>
61+
</ul>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Approach: Shortest Path + Dijkstra
2+
3+
# n = no. of rows, m = no. of cols
4+
# Time: O(mn log (mn))
5+
# Space: O(mn)
6+
7+
import heapq
8+
9+
class State:
10+
def __init__(self, x, y, dis):
11+
self.x = x
12+
self.y = y
13+
self.dis = dis
14+
15+
def __lt__(self, other):
16+
return self.dis < other.dis
17+
18+
19+
class Solution:
20+
def minTimeToReach(self, moveTime):
21+
n = len(moveTime)
22+
m = len(moveTime[0])
23+
inf = float("inf")
24+
d = [[inf] * m for _ in range(n)]
25+
v = [[0] * m for _ in range(n)]
26+
27+
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
28+
29+
d[0][0] = 0
30+
q = []
31+
heapq.heappush(q, State(0, 0, 0))
32+
33+
while q:
34+
s = heapq.heappop(q)
35+
if v[s.x][s.y]:
36+
continue
37+
if s.x == n - 1 and s.y == m - 1:
38+
break
39+
v[s.x][s.y] = 1
40+
for dx, dy in dirs:
41+
nx, ny = s.x + dx, s.y + dy
42+
if not (0 <= nx < n and 0 <= ny < m):
43+
continue
44+
dist = max(d[s.x][s.y], moveTime[nx][ny]) + (s.x + s.y) % 2 + 1
45+
if d[nx][ny] > dist:
46+
d[nx][ny] = dist
47+
heapq.heappush(q, State(nx, ny, dist))
48+
49+
return d[n - 1][m - 1]

0 commit comments

Comments
 (0)