-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b2e7bf
commit ab4c005
Showing
3 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/page-14/1466. Reorder Routes to Make All Paths Lead to the City Zero/minReorder.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { minReorder } from './minReorder'; | ||
|
||
describe('1466. Reorder Routes to Make All Paths Lead to the City Zero', () => { | ||
test('minReorder', () => { | ||
expect( | ||
minReorder(6, [ | ||
[0, 1], | ||
[1, 3], | ||
[2, 3], | ||
[4, 0], | ||
[4, 5], | ||
]), | ||
).toBe(3); | ||
|
||
expect( | ||
minReorder(5, [ | ||
[1, 0], | ||
[1, 2], | ||
[3, 2], | ||
[3, 4], | ||
]), | ||
).toBe(2); | ||
|
||
expect( | ||
minReorder(3, [ | ||
[1, 0], | ||
[2, 0], | ||
]), | ||
).toBe(0); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
src/page-14/1466. Reorder Routes to Make All Paths Lead to the City Zero/minReorder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type MinReorder = (n: number, connections: number[][]) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const minReorder: MinReorder = (n, connections) => { | ||
// Create an adjacency list | ||
const graph: Map<number, { to: number; originalDirection: boolean }[]> = new Map(); | ||
|
||
// Build the bidirectional graph, noting the original directions | ||
for (const [u, v] of connections) { | ||
// Ensure both nodes u and v exist in the graph | ||
graph.set(u, [...(graph.get(u) ?? []), { to: v, originalDirection: true }]); | ||
graph.set(v, [...(graph.get(v) ?? []), { to: u, originalDirection: false }]); | ||
} | ||
|
||
let changes = 0; | ||
const visited = new Set<number>(); | ||
|
||
// DFS function to explore the graph | ||
function dfs(node: number) { | ||
visited.add(node); | ||
|
||
for (const { to, originalDirection } of graph.get(node) ?? []) { | ||
if (!visited.has(to)) { | ||
// If the edge is originally directed away from node 0, increment the change count | ||
if (originalDirection) changes += 1; | ||
|
||
// Recursively apply DFS on the neighbor | ||
dfs(to); | ||
} | ||
} | ||
} | ||
|
||
// Start DFS from node 0 | ||
dfs(0); | ||
|
||
return changes; | ||
}; |