Skip to content

Commit 5490d68

Browse files
authored
Modified code as in the video
File(s) Modified: cheapest_flights_within_k_stops.cpp Language(s) Used: C++ Submission URL: https://leetcode.com/problems/cheapest-flights-within-k-stops/submissions/821560839/
1 parent fd465a1 commit 5490d68

File tree

1 file changed

+19
-65
lines changed

1 file changed

+19
-65
lines changed

cpp/neetcode_150/12_advanced_graphs/cheapest_flights_within_k_stops.cpp

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,31 @@
22
Given cities connected by flights [from,to,price], also given src, dst, & k:
33
Return cheapest price from src to dst with at most k stops
44
5-
Dijkstra's but modified, normal won't work b/c will discard heap nodes w/o finishing
6-
Modify: need to re-consider a node if dist from source is shorter than what we recorded
7-
But, if we encounter node already processed but # of stops from source is lesser,
8-
Need to add it back to the heap to be considered again
9-
10-
Time: O(V^2 log V) -> V = number of cities
11-
Space: O(V^2)
5+
Minimized implementation of the Bellman-Ford algorithm.
6+
Loop for #maximum-stops times and, for each flight, update the array of distances if
7+
there's a new path from the destination to the source having lower cost than the current.
8+
Time: O(k*n)
9+
Space: O(n)
1210
*/
1311

1412
class Solution {
1513
public:
1614
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
17-
// build adjacency matrix
18-
vector<vector<int>> adj(n, vector<int>(n));
19-
for (int i = 0; i < flights.size(); i++) {
20-
vector<int> flight = flights[i];
21-
adj[flight[0]][flight[1]] = flight[2];
22-
}
23-
24-
// shortest distances
25-
vector<int> distances(n, INT_MAX);
26-
distances[src] = 0;
27-
// shortest steps
28-
vector<int> currStops(n, INT_MAX);
29-
currStops[src] = 0;
30-
31-
// priority queue -> (cost, node, stops)
32-
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;
33-
pq.push({0, src, 0});
34-
35-
while (!pq.empty()) {
36-
int cost = pq.top()[0];
37-
int node = pq.top()[1];
38-
int stops = pq.top()[2];
39-
pq.pop();
40-
41-
// if destination is reached, return cost to get here
42-
if (node == dst) {
43-
return cost;
44-
}
45-
46-
// if no more steps left, continue
47-
if (stops == k + 1) {
48-
continue;
49-
}
50-
51-
// check & relax all neighboring edges
52-
for (int neighbor = 0; neighbor < n; neighbor++) {
53-
if (adj[node][neighbor] > 0) {
54-
int currCost = cost;
55-
int neighborDist = distances[neighbor];
56-
int neighborWeight = adj[node][neighbor];
57-
58-
// check if better cost
59-
int currDist = currCost + neighborWeight;
60-
if (currDist < neighborDist || stops + 1 < currStops[neighbor]) {
61-
pq.push({currDist, neighbor, stops + 1});
62-
distances[neighbor] = currDist;
63-
currStops[neighbor] = stops;
64-
} else if (stops < currStops[neighbor]) {
65-
// check if better steps
66-
pq.push({currDist, neighbor, stops + 1});
67-
}
68-
currStops[neighbor] = stops;
69-
}
15+
vector<int> dist(n, INT_MAX);
16+
dist[src] = 0;
17+
18+
for (int stops = 0; stops <= k; ++stops){
19+
vector<int> tmp = dist;
20+
for (auto& flight : flights){
21+
int s = flight[0], d = flight[1], p = flight[2];
22+
if (dist[s] == INT_MAX)
23+
continue;
24+
if (tmp[d] > dist[s] + p)
25+
tmp[d] = dist[s] + p;
7026
}
27+
dist = tmp;
7128
}
72-
73-
if (distances[dst] == INT_MAX) {
74-
return -1;
75-
}
76-
return distances[dst];
29+
30+
return dist[dst] == INT_MAX ? -1 : dist[dst];
7731
}
7832
};

0 commit comments

Comments
 (0)