Skip to content

Commit 2dc3c96

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

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77
* @return {number}
88
*/
99
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];
10+
let cost = Array(n).fill(Infinity)
11+
cost[src] = 0
12+
13+
for(let i = 0; i <= k; i++) {
14+
const tmp = [...cost]
15+
for(const [f, t, p] of flights) {
16+
if(cost[f] === Infinity) continue
17+
tmp[t] = Math.min(tmp[t], cost[f] + p)
18+
}
19+
cost = tmp
20+
}
21+
22+
return cost[dst] === Infinity ? -1 : cost[dst]
2523
};
2624

25+
2726
// another
2827

2928
/**

0 commit comments

Comments
 (0)