Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1ad6706
Create: 84-Largest-Rectangle-in-Histogram.ts
Ykhan799 Sep 6, 2022
07a3d4a
Create: 4-Median-of-Two-Sorted-Arrays.ts
Ykhan799 Sep 6, 2022
1616f50
Add: 25-Reverse-Nodes-in-k-Group.ts
Ykhan799 Sep 6, 2022
f9e95be
Create: 1899-Merge-Triplets-to-Form-Target-Triplet.ts
Ykhan799 Sep 6, 2022
04066fa
Create: 10-Regular-Expression-Matching.ts
Ykhan799 Sep 7, 2022
9ae6057
Merge branch 'neetcode-gh:main' into main
Ykhan799 Sep 7, 2022
156bccb
Create: 134-Gas-Station.ts
Ykhan799 Sep 7, 2022
7c67774
Create: 55-Jump-Game.ts
Ykhan799 Sep 7, 2022
07753d0
Create: 45-Jump-Game-II.ts
Ykhan799 Sep 7, 2022
ac3cef1
Create: 199-Binary-Tree-Right-Side-View.ts
Ykhan799 Sep 7, 2022
c661398
Create: 124-Binary-Tree-Maximum-Path-Sum.ts
Ykhan799 Sep 7, 2022
22ec063
Create: 98-Validate-Binary-Search-Tree.ts
Ykhan799 Sep 7, 2022
e6a0eb7
Create: 678-Valid-Parenthesis-String.ts
Ykhan799 Sep 8, 2022
9634ba8
Create: 518-Coin-Change-II.ts
Ykhan799 Sep 8, 2022
4509f59
Create: 97-Interleaving-String.ts
Ykhan799 Sep 8, 2022
b1a0f76
Create: 115-Distinct-Subsequences.ts
Ykhan799 Sep 8, 2022
db4585a
Create: 312-Burst-Balloons.ts
Ykhan799 Sep 8, 2022
62962a4
Create: 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.ts
Ykhan799 Sep 9, 2022
b740823
Create: 215-Kth-Largest-Element-in-an-Array.ts
Ykhan799 Sep 10, 2022
aec310a
Create: 621-Task-Scheduler.ts
Ykhan799 Sep 10, 2022
18e2450
Create: 994-Rotting-Oranges.ts
Ykhan799 Sep 10, 2022
70a7c8e
Create: 210-Course-Schedule-II.ts
Ykhan799 Sep 10, 2022
75af916
Create: 207-Course-Schedule.ts
Ykhan799 Sep 10, 2022
03c5b10
Merge branch 'neetcode-gh:main' into main
Ykhan799 Sep 10, 2022
1f08383
Create: 684-Redundant-Connection.ts
Ykhan799 Sep 10, 2022
ba94b3f
Create: 127-Word-Ladder.ts
Ykhan799 Sep 10, 2022
80e215d
Create: 787-Cheapest-Flights-Within-K-Stops.ts
Ykhan799 Sep 11, 2022
c82a5cb
Create: 494-Target-Sum.ts
Ykhan799 Sep 11, 2022
560f835
Create: 846-Hand-of-Straights.ts
Ykhan799 Sep 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create: 787-Cheapest-Flights-Within-K-Stops.ts
  • Loading branch information
Ykhan799 authored Sep 11, 2022
commit 80e215dab10aab1be3b668291249cc97f28bbe9f
40 changes: 40 additions & 0 deletions typescript/787-Cheapest-Flights-Within-K-Stops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function findCheapestPrice(n: number, flights: number[][], src: number, dst: number, k: number): number {
const adjacencyList = new Map();

for (let [start, end, cost] of flights) {
if (adjacencyList.has(start)) {
adjacencyList.get(start).push([end, cost]);
}
else {
adjacencyList.set(start, [[end, cost]]);
}
}

const queue = [[0, src, k+1]];
const visited = new Map();

while (queue.length) {
queue.sort((a, b) => a[0] - b[0]);

const [cost, city, stops] = queue.shift();
visited.set(city, stops);

if (city === dst) {
return cost;
}

if (stops <= 0 || !adjacencyList.has(city)) {
continue;
}

for (let [nextCity, nextCost] of adjacencyList.get(city)) {
if (visited.has(nextCity) && visited.get(nextCity) >= stops-1) {
continue;
}

queue.push([cost+nextCost, nextCity, stops-1]);
}
}

return -1;
};