Skip to content

Commit db59059

Browse files
committed
Add problem 787 - Cheapest Flights Within K Stops
1 parent a74597e commit db59059

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from collections import deque
2+
from typing import List
3+
4+
5+
class CheapestFlightsWithinKStops:
6+
@staticmethod
7+
def findCheapestPrice(n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
8+
# Adjacency list
9+
graph = []
10+
for i in range(n):
11+
graph.append([])
12+
for flight in flights:
13+
graph[flight[0]].append([flight[1], flight[2]])
14+
# Queue to perform BFS
15+
nodes = deque([[src, 0]])
16+
# Array to store minimum costs
17+
min_cost = [0x7fffffff] * n
18+
# Stops taken
19+
stops = 0
20+
# Process until queue is not empty and stops are less than equal to k
21+
while nodes and stops <= k:
22+
size = len(nodes)
23+
for i in range(size):
24+
current = nodes.popleft()
25+
# Process the neighbors of this current node
26+
for [node, cost] in graph[current[0]]:
27+
if (cost + current[1]) >= min_cost[node]:
28+
continue
29+
min_cost[node] = cost + current[1]
30+
nodes.append([node, min_cost[node]])
31+
stops += 1
32+
return min_cost[dst] if min_cost[dst] != 0x7fffffff else -1
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import unittest
2+
3+
from problems.graph.cheapest_flights_within_k_stops import CheapestFlightsWithinKStops
4+
5+
6+
class TestCheapestFlightsWithinKStops(unittest.TestCase):
7+
8+
def test_basic_scenario(self):
9+
cheapest_flight_within_k_flights = CheapestFlightsWithinKStops()
10+
n = 3
11+
flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
12+
src = 0
13+
dst = 2
14+
k = 1
15+
result = cheapest_flight_within_k_flights.findCheapestPrice(n, flights, src, dst, k)
16+
self.assertEqual(result, 200)
17+
18+
def test_no_available_flight(self):
19+
cheapest_flight_within_k_flights = CheapestFlightsWithinKStops()
20+
n = 3
21+
flights = [[0, 1, 100]]
22+
src = 0
23+
dst = 2
24+
k = 1
25+
result = cheapest_flight_within_k_flights.findCheapestPrice(n, flights, src, dst, k)
26+
self.assertEqual(result, -1)
27+
28+
def test_direct_flight(self):
29+
cheapest_flight_within_k_flights = CheapestFlightsWithinKStops()
30+
n = 3
31+
flights = [[0, 2, 300]]
32+
src = 0
33+
dst = 2
34+
k = 1
35+
result = cheapest_flight_within_k_flights.findCheapestPrice(n, flights, src, dst, k)
36+
self.assertEqual(result, 300)
37+
38+
def test_no_stop_allowed(self):
39+
cheapest_flight_within_k_flights = CheapestFlightsWithinKStops()
40+
n = 3
41+
flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
42+
src = 0
43+
dst = 2
44+
k = 0
45+
result = cheapest_flight_within_k_flights.findCheapestPrice(n, flights, src, dst, k)
46+
self.assertEqual(result, 500)
47+
48+
def test_large_k_value(self):
49+
cheapest_flight_within_k_flights = CheapestFlightsWithinKStops()
50+
n = 4
51+
flights = [[0, 1, 100], [1, 2, 100], [2, 3, 100], [0, 3, 500]]
52+
src = 0
53+
dst = 3
54+
k = 2
55+
result = cheapest_flight_within_k_flights.findCheapestPrice(n, flights, src, dst, k)
56+
self.assertEqual(result, 300)
57+
58+
59+
if __name__ == '__main__':
60+
unittest.main()

0 commit comments

Comments
 (0)