|
| 1 | +# 787. Cheapest Flights Within K Stops - Best Practices of LeetCode Solutions |
| 2 | +LeetCode link: [787. Cheapest Flights Within K Stops](https://leetcode.com/problems/cheapest-flights-within-k-stops), difficulty: **Medium**. |
| 3 | + |
| 4 | +## LeetCode description of "787. Cheapest Flights Within K Stops" |
| 5 | +There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [from_i, to_i, price_i]` indicates that there is a flight from city `from_i` to city `to_i` with cost `price_i`. |
| 6 | + |
| 7 | +You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from `src` to `dst` with at most `k` stops_. If there is no such route, return `-1`. |
| 8 | + |
| 9 | +### [Example 1] |
| 10 | + |
| 11 | + |
| 12 | +**Input**: `n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1` |
| 13 | + |
| 14 | +**Output**: `700` |
| 15 | + |
| 16 | +**Explanation**: |
| 17 | +``` |
| 18 | +The graph is shown above. |
| 19 | +The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700. |
| 20 | +Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops. |
| 21 | +``` |
| 22 | + |
| 23 | +### [Example 2] |
| 24 | +**Input**: `n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1` |
| 25 | + |
| 26 | +**Output**: `200` |
| 27 | + |
| 28 | +**Explanation**: |
| 29 | +``` |
| 30 | +The graph is shown above. |
| 31 | +The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200. |
| 32 | +``` |
| 33 | + |
| 34 | +### [Example 3] |
| 35 | +**Input**: `n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0` |
| 36 | + |
| 37 | +**Output**: `500` |
| 38 | + |
| 39 | +**Explanation**: |
| 40 | +``` |
| 41 | +The graph is shown above. |
| 42 | +The optimal path with no stops from city 0 to 2 is marked in red and has cost 500. |
| 43 | +``` |
| 44 | + |
| 45 | +### [Constraints] |
| 46 | +- `1 <= n <= 100` |
| 47 | +- `0 <= flights.length <= (n * (n - 1) / 2)` |
| 48 | +- `flights[i].length == 3` |
| 49 | +- `0 <= from_i, to_i < n` |
| 50 | +- `from_i != to_i` |
| 51 | +- `1 <= price_i <= 10000` |
| 52 | +- There will not be any multiple flights between two cities. |
| 53 | +- `0 <= src, dst, k < n` |
| 54 | +- `src != dst` |
| 55 | + |
| 56 | +## Intuition |
| 57 | +We can solve it via **Bellman-Ford algorithm**. |
| 58 | + |
| 59 | +For a detailed introduction to `Bellman-Ford algorithm`, please refer to [743. Network Delay Time](./743-network-delay-time.md). |
| 60 | + |
| 61 | +## Complexity |
| 62 | +**V**: vertex count, **E**: Edge count. |
| 63 | + |
| 64 | +* Time: `O(K * E)`. |
| 65 | +* Space: `O(V)`. |
| 66 | + |
| 67 | +## Python |
| 68 | +```python |
| 69 | +class Solution: |
| 70 | + def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: |
| 71 | + min_costs = [float('inf')] * n |
| 72 | + min_costs[src] = 0 |
| 73 | + |
| 74 | + for _ in range(k + 1): |
| 75 | + min_costs_clone = min_costs.copy() |
| 76 | + |
| 77 | + for from_city, to_city, price in flights: |
| 78 | + if min_costs_clone[from_city] == float('inf'): |
| 79 | + continue |
| 80 | + |
| 81 | + cost = min_costs_clone[from_city] + price |
| 82 | + |
| 83 | + if cost < min_costs[to_city]: |
| 84 | + min_costs[to_city] = cost |
| 85 | + |
| 86 | + if min_costs[dst] == float('inf'): |
| 87 | + return -1 |
| 88 | + |
| 89 | + return min_costs[dst] |
| 90 | +``` |
| 91 | + |
| 92 | +## JavaScript |
| 93 | +```javascript |
| 94 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 95 | +``` |
| 96 | + |
| 97 | +## Java |
| 98 | +```java |
| 99 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 100 | +``` |
| 101 | + |
| 102 | +## C++ |
| 103 | +```cpp |
| 104 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 105 | +``` |
| 106 | + |
| 107 | +## C# |
| 108 | +```c# |
| 109 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 110 | +``` |
| 111 | + |
| 112 | +## Go |
| 113 | +```go |
| 114 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 115 | +``` |
| 116 | + |
| 117 | +## Ruby |
| 118 | +```ruby |
| 119 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 120 | +``` |
| 121 | + |
| 122 | +## C, Kotlin, Swift, Rust or other languages |
| 123 | +``` |
| 124 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 125 | +``` |
0 commit comments