Skip to content

Commit b60ccce

Browse files
committed
Sync LeetCode submission Runtime - 17 ms (55.94%), Memory - 19.8 MB (21.91%)
1 parent 4b224e7 commit b60ccce

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<p>You are given a positive integer <code>n</code> which is the number of nodes of a <strong>0-indexed directed weighted</strong> graph and a <strong>0-indexed</strong> <strong>2D array</strong> <code>edges</code> where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
2+
3+
<p>You are also given a node <code>s</code> and a node array <code>marked</code>; your task is to find the <strong>minimum</strong> distance from <code>s</code> to <strong>any</strong> of the nodes in <code>marked</code>.</p>
4+
5+
<p>Return <em>an integer denoting the minimum distance from </em><code>s</code><em> to any node in </em><code>marked</code><em> or </em><code>-1</code><em> if there are no paths from s to any of the marked nodes</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> n = 4, edges = [[0,1,1],[1,2,3],[2,3,2],[0,3,4]], s = 0, marked = [2,3]
12+
<strong>Output:</strong> 4
13+
<strong>Explanation:</strong> There is one path from node 0 (the green node) to node 2 (a red node), which is 0-&gt;1-&gt;2, and has a distance of 1 + 3 = 4.
14+
There are two paths from node 0 to node 3 (a red node), which are 0-&gt;1-&gt;2-&gt;3 and 0-&gt;3, the first one has a distance of 1 + 3 + 2 = 6 and the second one has a distance of 4.
15+
The minimum of them is 4.
16+
</pre>
17+
18+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/06/13/image_2023-06-13_16-34-38.png" style="width: 185px; height: 180px;" /></p>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> n = 5, edges = [[0,1,2],[0,2,4],[1,3,1],[2,3,3],[3,4,2]], s = 1, marked = [0,4]
24+
<strong>Output:</strong> 3
25+
<strong>Explanation:</strong> There are no paths from node 1 (the green node) to node 0 (a red node).
26+
There is one path from node 1 to node 4 (a red node), which is 1-&gt;3-&gt;4, and has a distance of 1 + 2 = 3.
27+
So the answer is 3.
28+
</pre>
29+
30+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/06/13/image_2023-06-13_16-35-13.png" style="width: 300px; height: 285px;" /></p>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> n = 4, edges = [[0,1,1],[1,2,3],[2,3,2]], s = 3, marked = [0,1]
36+
<strong>Output:</strong> -1
37+
<strong>Explanation:</strong> There are no paths from node 3 (the green node) to any of the marked nodes (the red nodes), so the answer is -1.
38+
</pre>
39+
40+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/06/13/image_2023-06-13_16-35-47.png" style="width: 420px; height: 80px;" /></p>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= n &lt;= 500</code></li>
47+
<li><code>1 &lt;= edges.length &lt;= 10<sup>4</sup></code></li>
48+
<li><code>edges[i].length = 3</code></li>
49+
<li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li>
50+
<li><code>1 &lt;= edges[i][2] &lt;=&nbsp;10<sup>6</sup></code></li>
51+
<li><code>1 &lt;= marked.length&nbsp;&lt;= n - 1</code></li>
52+
<li><code>0 &lt;= s, marked[i]&nbsp;&lt;= n - 1</code></li>
53+
<li><code>s != marked[i]</code></li>
54+
<li><code>marked[i] != marked[j]</code> for every <code>i != j</code></li>
55+
<li>The&nbsp;graph might have&nbsp;<strong>repeated edges</strong>.</li>
56+
<li>The graph is generated such that it has no&nbsp;<strong>self-loops</strong>.</li>
57+
</ul>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Approach 1: Dijkstra's Algorithm
2+
3+
# m = len(edges)
4+
# Time: O((n + m) log n)
5+
# Space: O(n + m)
6+
7+
from collections import defaultdict
8+
import heapq
9+
10+
class Solution:
11+
def minimumDistance(self, n: int, edges: List[List[int]], s: int, marked: List[int]) -> int:
12+
mark_set = set(marked)
13+
adj = defaultdict(list)
14+
15+
for u, v, w in edges:
16+
adj[u].append((v, w))
17+
18+
dist = {s: 0}
19+
20+
min_heap = [(0, s)]
21+
22+
while min_heap:
23+
distance, node = heapq.heappop(min_heap)
24+
25+
if node in mark_set:
26+
return dist[node]
27+
28+
for next_node, weight in adj[node]:
29+
new_dist = distance + weight
30+
31+
if new_dist < dist.get(next_node, float('inf')):
32+
dist[next_node] = new_dist
33+
heapq.heappush(min_heap, (new_dist, next_node))
34+
35+
return -1
36+
37+

0 commit comments

Comments
 (0)