Skip to content

Commit 75eda4b

Browse files
author
Hamid Gasmi
committed
#302: add time complexity analysis for BFS-like solution
1 parent 4adb4b7 commit 75eda4b

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

09-problems/lc_787_cheapest_flights_within_k_stops.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
2222
2. Intuition:
2323
- Naive:
24-
- Kind of BFS + Optimizations:
24+
- Kind of BFS + Optimizations: O(|V|^2 + |V||E|) ?
2525
- to have a variable dst cheapest path price (initialized to Infinity)
2626
- To visit cities multiple-times: when curr visit path price < prev visit path price
2727
- To stop a path if its price > dst cheapest path price
28-
- Each node
2928
- We could do better:
3029
3130
3. Implementation
@@ -39,9 +38,9 @@
3938

4039
class Solution_BFS:
4140
'''
42-
Time Complexity:
41+
Time Complexity: O(|V|^2 + |V||E|)
4342
T = T(Build Adjacency List) + T(Kind Of BFS algo)
44-
= O(|E|)
43+
= O(|E|) + |V| * O(|V| + |E|)
4544
4645
Space Complexity: O(|V| + |E|)
4746
S = S(Adjacency_list) + S(BFS Curr Level Queue) + S(BFS Next Level Queue) + S(Visited Hash Map)

0 commit comments

Comments
 (0)