LeetCode: Keys And Rooms Solution
1/**2 * @param {number[][]} rooms3 * @return {boolean}4 */5var canVisitAllRooms = function (rooms) {6 const N = rooms.length7 const visited = Array(N).fill(false)89 const dfs = roomIndex => {10 if (visited[roomIndex]) {11 return12 }13 visited[roomIndex] = true14 for (const roomKey of rooms[roomIndex]) {15 dfs(roomKey)16 }17 }1819 dfs(0)2021 return visited.filter(Boolean).length === N22}
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.