Skip to content

Commit e8d627d

Browse files
committed
[LeetCode Sync] Runtime - 7 ms (69.25%), Memory - 19.1 MB (68.84%)
1 parent 343edb3 commit e8d627d

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>There are <code>n</code> cities connected by some number of flights. You are given an array <code>flights</code> where <code>flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>]</code> indicates that there is a flight from city <code>from<sub>i</sub></code> to city <code>to<sub>i</sub></code> with cost <code>price<sub>i</sub></code>.</p>
2+
3+
<p>You are also given three integers <code>src</code>, <code>dst</code>, and <code>k</code>, return <em><strong>the cheapest price</strong> from </em><code>src</code><em> to </em><code>dst</code><em> with at most </em><code>k</code><em> stops. </em>If there is no such route, return<em> </em><code>-1</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-3drawio.png" style="width: 332px; height: 392px;" />
8+
<pre>
9+
<strong>Input:</strong> n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
10+
<strong>Output:</strong> 700
11+
<strong>Explanation:</strong>
12+
The graph is shown above.
13+
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
14+
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-1drawio.png" style="width: 332px; height: 242px;" />
19+
<pre>
20+
<strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
21+
<strong>Output:</strong> 200
22+
<strong>Explanation:</strong>
23+
The graph is shown above.
24+
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
25+
</pre>
26+
27+
<p><strong class="example">Example 3:</strong></p>
28+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/18/cheapest-flights-within-k-stops-2drawio.png" style="width: 332px; height: 242px;" />
29+
<pre>
30+
<strong>Input:</strong> n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
31+
<strong>Output:</strong> 500
32+
<strong>Explanation:</strong>
33+
The graph is shown above.
34+
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= n &lt;= 100</code></li>
42+
<li><code>0 &lt;= flights.length &lt;= (n * (n - 1) / 2)</code></li>
43+
<li><code>flights[i].length == 3</code></li>
44+
<li><code>0 &lt;= from<sub>i</sub>, to<sub>i</sub> &lt; n</code></li>
45+
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
46+
<li><code>1 &lt;= price<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
47+
<li>There will not be any multiple flights between two cities.</li>
48+
<li><code>0 &lt;= src, dst, k &lt; n</code></li>
49+
<li><code>src != dst</code></li>
50+
</ul>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution:
2+
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
3+
# graph = defaultdict(list)
4+
# for source, dest, price in flights:
5+
# graph[source].append((dest, price))
6+
7+
# @cache
8+
# def dfs(source: int, k: int) -> int:
9+
# if source == dst:
10+
# return 0
11+
# if k <= 0:
12+
# return float('inf')
13+
14+
# k -= 1
15+
# ans = float('inf')
16+
# for dest, price in graph[source]:
17+
# ans = min(ans, dfs(dest, k) + price)
18+
# return ans
19+
20+
# result = dfs(src, k + 1)
21+
22+
# return -1 if result == float('inf') else result
23+
24+
# Bellman-Ford approach
25+
dist = [float('inf')] * n
26+
dist[src] = 0
27+
28+
for _ in range(k + 1):
29+
changed = False
30+
new_dists = dist[:]
31+
32+
for source, dest, price in flights:
33+
if dist[source] != float('inf') and new_dists[dest] > dist[source] + price:
34+
new_dists[dest] = dist[source] + price
35+
changed = True
36+
37+
dist = new_dists
38+
if not changed:
39+
break
40+
41+
return -1 if dist[dst] == float('inf') else dist[dst]

0 commit comments

Comments
 (0)