Skip to content

Commit 68cf95b

Browse files
authored
Update 787-cheapest-flights-within-k-stops.js
1 parent c3e8486 commit 68cf95b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

787-cheapest-flights-within-k-stops.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} flights
4+
* @param {number} src
5+
* @param {number} dst
6+
* @param {number} k
7+
* @return {number}
8+
*/
9+
var findCheapestPrice = function(n, flights, src, dst, k) {
10+
let cost = new Array(n).fill(Number.MAX_SAFE_INTEGER);
11+
cost[src] = 0;
12+
13+
for (let i = 0; i <= k; i++) {
14+
let temp = [...cost];
15+
for (let [curr, next, price] of flights) {
16+
if (cost[curr] === Number.MAX_SAFE_INTEGER) {
17+
continue;
18+
}
19+
temp[next] = Math.min(temp[next], cost[curr] + price);
20+
}
21+
cost = temp;
22+
}
23+
24+
return cost[dst] === Number.MAX_SAFE_INTEGER ? -1 : cost[dst];
25+
};
26+
27+
// another
28+
129
/**
230
* @param {number} n
331
* @param {number[][]} flights

0 commit comments

Comments
 (0)