Skip to content

Commit

Permalink
205th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 15, 2024
1 parent 1b2e7bf commit ab4c005
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,16 @@ Ace Coding Interview with 75 Qs
[700]: ./src/page-7/700.%20Search%20in%20a%20Binary%20Search%20Tree/searchBST.ts
[450]: ./src/page-5/450.%20Delete%20Node%20in%20a%20BST/deleteNode.ts

| Graphs - DFS | | |
| ------------------------------------------------------------ | --------------- | ------ |
| 841. Keys and Rooms | [Solution][841] | Medium |
| 547. Number of Provinces | [Solution][547] | Medium |
| 1466. Reorder Routes to Make All Paths Lead to the City Zero | Solution | Medium |
| 399. Evaluate Division | Solution | Medium |
| Graphs - DFS | | |
| ------------------------------------------------------------ | ---------------- | ------ |
| 841. Keys and Rooms | [Solution][841] | Medium |
| 547. Number of Provinces | [Solution][547] | Medium |
| 1466. Reorder Routes to Make All Paths Lead to the City Zero | [Solution][1466] | Medium |
| 399. Evaluate Division | Solution | Medium |

[841]: ./src/page-8/841.%20Keys%20and%20Rooms/canVisitAllRooms.ts
[547]: ./src/page-6/547.%20Number%20of%20Provinces/findCircleNum.ts
[1466]: ./src/page-14/1466.%20Reorder%20Routes%20to%20Make%20All%20Paths%20Lead%20to%20the%20City%20Zero/minReorder.ts

| Graphs - BFS | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
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);
});
});
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;
};

0 comments on commit ab4c005

Please sign in to comment.