|
| 1 | +# 787. [Cheapest Flights Within K Stops](https://leetcode.com/problems/cheapest-flights-within-k-stops/) |
| 2 | + |
| 3 | +## Approach 1: DFS with Pruning |
| 4 | + |
| 5 | +### Solution |
| 6 | +```python |
| 7 | +# Time Complexity: O(n^k) in the worst case |
| 8 | +# Space Complexity: O(n) |
| 9 | +from collections import defaultdict |
| 10 | + |
| 11 | +class Solution: |
| 12 | + def __init__(self): |
| 13 | + self.result = float('inf') |
| 14 | + |
| 15 | + def findCheapestPrice(self, n, flights, src, dst, K): |
| 16 | + # Create adjacency map for graph edges |
| 17 | + graph = defaultdict(dict) |
| 18 | + for u, v, price in flights: |
| 19 | + graph[u][v] = price |
| 20 | + |
| 21 | + # Perform DFS |
| 22 | + self.dfs(graph, src, dst, K + 1, 0) |
| 23 | + |
| 24 | + return -1 if self.result == float('inf') else self.result |
| 25 | + |
| 26 | + def dfs(self, graph, node, dst, stops, cost): |
| 27 | + if node == dst: |
| 28 | + self.result = cost |
| 29 | + return |
| 30 | + |
| 31 | + if stops == 0: |
| 32 | + return # No more stops available |
| 33 | + |
| 34 | + if node not in graph: |
| 35 | + return # No outgoing flights |
| 36 | + |
| 37 | + for neighbor, price in graph[node].items(): |
| 38 | + # Pruning: proceed only if this path is cheaper than the best found so far |
| 39 | + if cost + price > self.result: |
| 40 | + continue |
| 41 | + |
| 42 | + self.dfs(graph, neighbor, dst, stops - 1, cost + price) |
| 43 | +``` |
| 44 | + |
| 45 | +## Approach 2: Bellman-Ford Algorithm |
| 46 | + |
| 47 | +### Solution |
| 48 | +```python |
| 49 | +# Time Complexity: O(K * E), where E is the number of edges |
| 50 | +# Space Complexity: O(n) |
| 51 | +import sys |
| 52 | + |
| 53 | +class Solution: |
| 54 | + def findCheapestPrice(self, n, flights, src, dst, K): |
| 55 | + prices = [sys.maxsize] * n |
| 56 | + prices[src] = 0 |
| 57 | + |
| 58 | + # Relax the edges up to K+1 times |
| 59 | + for _ in range(K + 1): |
| 60 | + temp_prices = prices[:] |
| 61 | + for u, v, price in flights: |
| 62 | + if prices[u] == sys.maxsize: |
| 63 | + continue |
| 64 | + |
| 65 | + if prices[u] + price < temp_prices[v]: |
| 66 | + temp_prices[v] = prices[u] + price |
| 67 | + prices = temp_prices |
| 68 | + |
| 69 | + return -1 if prices[dst] == sys.maxsize else prices[dst] |
| 70 | +``` |
| 71 | + |
| 72 | +## Approach 3: Dijkstra's Algorithm with Priority Queue |
| 73 | + |
| 74 | +### Solution |
| 75 | +```python |
| 76 | +# Time Complexity: O(E + VlogV), where E is the number of edges and V is the number of vertices |
| 77 | +# Space Complexity: O(n) |
| 78 | +import heapq |
| 79 | +from collections import defaultdict |
| 80 | + |
| 81 | +class Solution: |
| 82 | + def findCheapestPrice(self, n, flights, src, dst, K): |
| 83 | + # Create an adjacency list for the graph |
| 84 | + graph = defaultdict(list) |
| 85 | + for u, v, price in flights: |
| 86 | + graph[u].append((v, price)) |
| 87 | + |
| 88 | + # Priority queue will hold entries of (cost, node, stops remaining) |
| 89 | + pq = [(0, src, K + 1)] |
| 90 | + |
| 91 | + while pq: |
| 92 | + cost, node, stops_remaining = heapq.heappop(pq) |
| 93 | + |
| 94 | + if node == dst: |
| 95 | + return cost |
| 96 | + |
| 97 | + if stops_remaining > 0: |
| 98 | + if node not in graph: |
| 99 | + continue |
| 100 | + for next_node, price_to_next in graph[node]: |
| 101 | + heapq.heappush(pq, (cost + price_to_next, next_node, stops_remaining - 1)) |
| 102 | + |
| 103 | + return -1 # No valid path found |
| 104 | +``` |
| 105 | + |
0 commit comments