LeetCode: Keys And Rooms Solution

1/**
2 * @param {number[][]} rooms
3 * @return {boolean}
4 */
5var canVisitAllRooms = function (rooms) {
6 const N = rooms.length
7 const visited = Array(N).fill(false)
8
9 const dfs = roomIndex => {
10 if (visited[roomIndex]) {
11 return
12 }
13 visited[roomIndex] = true
14 for (const roomKey of rooms[roomIndex]) {
15 dfs(roomKey)
16 }
17 }
18
19 dfs(0)
20
21 return visited.filter(Boolean).length === N
22}

Comments

Loading comments...

Tags

leetcode

dfs

graph

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Number Of Orders In The Backlog

Mar 21, 2021

Previous Post

HoningJS

Search Posts