Skip to content

Commit 5412d19

Browse files
committed
Sync LeetCode submission Runtime - 10 ms (98.44%), Memory - 25.1 MB (68.75%)
1 parent 8cb97c9 commit 5412d19

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<p>You are in a city that consists of <code>n</code> intersections numbered from <code>0</code> to <code>n - 1</code> with <strong>bi-directional</strong> roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most one road between any two intersections.</p>
2+
3+
<p>You are given an integer <code>n</code> and a 2D integer array <code>roads</code> where <code>roads[i] = [u<sub>i</sub>, v<sub>i</sub>, time<sub>i</sub>]</code> means that there is a road between intersections <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> that takes <code>time<sub>i</sub></code> minutes to travel. You want to know in how many ways you can travel from intersection <code>0</code> to intersection <code>n - 1</code> in the <strong>shortest amount of time</strong>.</p>
4+
5+
<p>Return <em>the <strong>number of ways</strong> you can arrive at your destination in the <strong>shortest amount of time</strong></em>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2025/02/14/1976_corrected.png" style="width: 255px; height: 400px;" />
10+
<pre>
11+
<strong>Input:</strong> n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]]
12+
<strong>Output:</strong> 4
13+
<strong>Explanation:</strong> The shortest amount of time it takes to go from intersection 0 to intersection 6 is 7 minutes.
14+
The four ways to get there in 7 minutes are:
15+
- 0 ➝ 6
16+
- 0 ➝ 4 ➝ 6
17+
- 0 ➝ 1 ➝ 2 ➝ 5 ➝ 6
18+
- 0 ➝ 1 ➝ 3 ➝ 5 ➝ 6
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> n = 2, roads = [[1,0,10]]
25+
<strong>Output:</strong> 1
26+
<strong>Explanation:</strong> There is only one way to go from intersection 0 to intersection 1, and it takes 10 minutes.
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= n &lt;= 200</code></li>
34+
<li><code>n - 1 &lt;= roads.length &lt;= n * (n - 1) / 2</code></li>
35+
<li><code>roads[i].length == 3</code></li>
36+
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n - 1</code></li>
37+
<li><code>1 &lt;= time<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
38+
<li><code>u<sub>i </sub>!= v<sub>i</sub></code></li>
39+
<li>There is at most one road connecting any two intersections.</li>
40+
<li>You can reach any intersection from any other intersection.</li>
41+
</ul>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Approach 1: Dijkstra's Algorithm
2+
3+
# e = total no. of edges
4+
# Time: O((n + e) log n)
5+
# Space: O(n + e)
6+
7+
import heapq
8+
9+
class Solution:
10+
def countPaths(self, n: int, roads: List[List[int]]) -> int:
11+
MOD = 10 ** 9 + 7
12+
13+
graph = [[] for _ in range(n)]
14+
15+
for start_node, end_node, travel_time in roads:
16+
graph[start_node].append((end_node, travel_time))
17+
graph[end_node].append((start_node, travel_time))
18+
19+
min_heap = [(0, 0)] # (time, node)
20+
shortest_time = [float('inf')] * n
21+
path_count = [0] * n # No. of ways to reach node in shortest time
22+
23+
shortest_time[0] = 0
24+
path_count[0] = 1
25+
26+
while min_heap:
27+
curr_time, curr_node = heapq.heappop(min_heap)
28+
if curr_time > shortest_time[curr_node]:
29+
continue
30+
31+
for neighbor_node, road_time in graph[curr_node]:
32+
# Found a new shortest path → Update shortest time and reset path count
33+
if curr_time + road_time < shortest_time[neighbor_node]:
34+
shortest_time[neighbor_node] = curr_time + road_time
35+
path_count[neighbor_node] = path_count[curr_node]
36+
heapq.heappush(min_heap, (shortest_time[neighbor_node], neighbor_node))
37+
38+
# Found another way with the same shortest time → Add to path count
39+
elif curr_time + road_time == shortest_time[neighbor_node]:
40+
path_count[neighbor_node] = (path_count[neighbor_node] + path_count[curr_node]) % MOD
41+
42+
return path_count[n - 1]
43+

0 commit comments

Comments
 (0)