-
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
1cc889f
commit 53af702
Showing
3 changed files
with
38 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
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,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); | ||
}); | ||
}); |
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,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; | ||
}; |