Created
June 24, 2018 03:33
-
-
Save bruceoutdoors/7b2da1c7af0c9f7797eb61787e515908 to your computer and use it in GitHub Desktop.
My solution to https://leetcode.com/problems/keys-and-rooms
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
public class Solution { | |
IList<IList<int>> graph; | |
bool[] marked; | |
public bool CanVisitAllRooms(IList<IList<int>> rooms) { | |
graph = rooms; | |
marked = new bool[rooms.Count]; | |
dfs(0); | |
foreach (bool b in marked) { | |
if (!b) return false; | |
} | |
return true; | |
} | |
private void dfs(int v) { | |
marked[v] = true; | |
foreach (int w in graph[v]) { | |
if (!marked[w]) { | |
dfs(w); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment