Skip to content

Commit

Permalink
203rd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 8, 2024
1 parent 1cc889f commit 53af702
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,14 @@ 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 | Medium |
| 547. Number of Provinces | Solution | 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 | Medium |
| 1466. Reorder Routes to Make All Paths Lead to the City Zero | Solution | Medium |
| 399. Evaluate Division | Solution | Medium |

[841]: ./src/page-8/841.%20Keys%20and%20Rooms/canVisitAllRooms.ts

| Graphs - BFS | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
8 changes: 8 additions & 0 deletions src/page-8/841. Keys and Rooms/canVisitAllRooms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { canVisitAllRooms } from './canVisitAllRooms';

describe('841. Keys and Rooms', () => {
test('canVisitAllRooms', () => {
expect(canVisitAllRooms([[1], [2], [3], []])).toBe(true);
expect(canVisitAllRooms([[1, 3], [3, 0, 1], [2], [0]])).toBe(false);
});
});
22 changes: 22 additions & 0 deletions src/page-8/841. Keys and Rooms/canVisitAllRooms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type CanVisitAllRooms = (rooms: number[][]) => boolean;

/**
* Accepted
*/
export const canVisitAllRooms: CanVisitAllRooms = (rooms) => {
const visited = new Set<number>();

function dfs(room: number) {
visited.add(room);

for (const key of rooms[room]) {
if (!visited.has(key)) {
dfs(key);
}
}
}

dfs(0);

return visited.size === rooms.length;
};

0 comments on commit 53af702

Please sign in to comment.