DFS
Start room 0 to DFS all rooms. Use a set to keep track of the visited rooms. Return true if the visited number of rooms equals total number of rooms.
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
Set<Integer> visited = new HashSet<>();
dfs(0, rooms, visited);
return visited.size() == rooms.size();
}
private void dfs(int curt, List<List<Integer>> rooms, Set<Integer> visited) {
visited.add(curt);
if (rooms.get(curt) == null) return;
for (int next : rooms.get(curt)) {
if (visited.contains(next)) continue;
dfs(next, rooms, visited);
}
}public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
dfs(image, sr, sc, image[sr][sc], newColor);
return image;
}
private void dfs(int[][] image, int x, int y, int orginal, int newColor) {
if (x < 0 || x >= image.length || y < 0 || y >= image[0].length
|| image[x][y] != orginal
|| image[x][y] == newColor ) return;
image[x][y] = newColor;
dfs(image, x + 1, y, orginal, newColor);
dfs(image, x - 1, y, orginal, newColor);
dfs(image, x, y + 1, orginal, newColor);
dfs(image, x, y - 1, orginal, newColor);
}
Use a HashMap to store correlation. While traversing, if map contains this node, return the corresponding clone. Create clone, add the corresponding new clone to the neighbors list.
T: O(n)
BFS: link
DFS to fill the edge nodes and its linked nodes to a temporary state (eg. 'T'). Fill the middle nodes then fill all 'T's back to it is original state 'O'.
When reach a node of an Island, DFS all nodes and fill the grid with '0'. This would eliminate repeated traversal.
Distinctness can be determined by the shape of the island, however saving the shape might be a bit difficult. An easier way is to save the pattern of traversal.
Starting with top left corner fo the island, DFS and use a StringBuilder to save the directions. Save "0" at when breaking out the recursion
sb.append("0");
since it represents the pattern of recursive traversal, move both forward and backwards.
This problem is similar as other 'island on graph' problems. However, the DFS is quite tricky. You have to make sure all nodes are filled. Also the DFS of each level can't terminate early. For example:
res = dfs(grid, next_x, next_y) && res;
is the only way to complete the traversal. Other ways such as:
if (true) : return trueif (false): return falseres = res && dfs(grid, next_x, next_y)
will terminate the recursion early.
305. Number of Islands II305. Number of Islands II
DFS when see the ship
Solution2: one pass
Get number of mines of each grid. If mine count == 0, set board to 'B' and keep traversing. Otherwise set the grid to that mine number.
T: O( 8* (m*n) ^ 2)
Search words on that input String s to find if that set contains a prefix of s. Use memorization to eliminate redundant traversal.
T: O (n^2) S : O (n)
Solution2 : dp
Instead of return boolean, carry results.
T: O(n^2 + 2^n + W) W is number of words in the dictionary. S: O(2^n * n + W)
Build graph from edge. Traverse to see no repeated visited node. Also add count visited nodes to see if it equals n, which means there is no left over nodes.
T: O(n) S: O(n)
This is a weighted edge graph. Construct the graph of the equations and values. Traverse the graph by using DFS or BFS to query result. Starting with 1.0 and cumulate the weight of each traversal.
Backtracking and solve.
DFS to get the shortest path. Using Memo. Also cutting branch by only goes in one direction will help to reduce the total paths.
Store the longest path from current position x, y in a memo matrix.
Last updated
Was this helpful?