Problem Names Solution Writeups Code
Number of Islands

Create fixed size 2d vector as visited instead of unordered_set of pair in C++ will be much faster. The algo is like this, for each grid[i][j] == 1, if it is not visited, count + 1, then do dfs on it.

Clone Graph

Create a hash map that maps value to Node pointer to indicate that that node has been created. If it is not created, create it and add it to the hash map, then iterate through its neighbors.

Max Area of Island

Same as number of island question, except that now the dfs returns integer of area. Area = 1 + dfs() for 4 directions. Record the largest area.

Rotting Oranges

Do BFS in parallel, so inside while (!frontier.empty()), loop through each element in queue by keep popping the front element. Since it is possible to have more than 1 rotten orange in the start, push them to frontier at the beginning.

Pacific Atlantic Water Flow

The hint is that instead of iterating through each point to determine if it can go both direction, check from the two oceans to each point. Since we know the first row and first column can go pacific and last row and last column can go atlantic, continuously do DFS to check if each point can go. We need two 2D vectors to that act as a set to store the point that can flow to each ocean.

Course Schedule

Core concept is to detect if the course graph contains cycle. If yes return false, if no return true. Create a visited vector, that will mark a node as visited once it is processed. Then, for each node being processed, if it contains req that cannot be finished, return false for that node, else return true. To speed up the checking (to skip checking the requisites of the same course everytime iterating the loop), clear the pre-req of the course that can be complete, and check if a course req is empty, return true immediately.

Course Schedule II

Must do topological sort. The way to do topological sort is that we have 3 states for each node, 0 means the node is unexplored/unprocessed yet, 1 means the node is processed and it forms a cycle, 2 means the node is processed and it is okay. For each node do DFS to obtain the topological sort order. Output the order when every node is processed.

Surrounded Regions

Instead of checking each 'O', checks from the border cells, if it is a 'O', it is not captured, and then do dfs to check if there exists any connected 'O' cells, which are also not captured.

Redundant Connection

Since it is guaranteed to have n nodes, we can do union-find of size n nodes. Create a parent vector and rank vector. Initially parent of each node is itself, and each rank is 1. Do union-find to merge parents with larger rank, and then update the merged nodes correspondingly. When a node that hasn't been processed with same parent has been detected, simply returned that edge.

Walls and Gates

The problem is asking that for each empty space, count the min distance from itself to the nearest gate. To do this, simply do BFS for all gates concurrently, and update each neighboring empty space.

Number of Connected Components in Graph

The problem asks for counting the number of connected components in the graph. The fastest way to do this is by doing union-find. Update count only if two nodes at first have different parents. To know the number of connected components, do n - count.

Graph Valid Tree

The problem asks to check if a graph is also a tree. To fulfill this condition, we need to ensure the graph does not contain cycle and every node is connected. The algorithm to check for cycle is to create a visited set that will add visited node inside. If a node is revisited, return false. After ensuring no cycle, before return true, check if visited.size() == number of nodes, if not, returned false, since this indicates that not all nodes are connected.

Word Ladder

Create adjacency list where key is the word, and values are set of other words that differ by one character. Then do BFS to find minimum path, this will be O(n2m). Another way to generate adjacency list is by using the word pattern that is generated from each word, and then the value will be set of word that contains such pattern. This will give O(m2n) solution and is more suitable for this question.

Reconstruct Itinerary

Do DFS topological sort, add node in reversed order. For each destination in curr, just do DFS and explore new destination, if there is no more destination available, add it to the path. Remember to reverse the order once all DFS is accomplished.

Min Cost To Connect All Points

Do Prim's algorithm (like Dijkstra), C++ has its own way of doing. Create a vector of size n, keep update min cost of each position each time a min cost is found. Total time complexity will be O(n2).

Swim In Rising Water

The question is to find maximum along the minimum path among all the paths to goal. The idea is to do Dijkstra, and then record each time that is maximum among the explored nodes so far. Loop until frontier is empty or the goal point is visited.

Cheapest Flights Withint K Stops

Do Dijkstra, first compare the current stops number, then compare the cost. When reached goal, add to a vector. Once the frontier is empty, return the min among the vector. Skip the node if the current cost is more than the minimum cost recorded so far. (Do not simply check if it is visited, since it does not work for goal)

Alien Dictionary

The problem asks to find the order of alphabet lexicographically. Such order cannot be found if there exists contradiction, where larger word length appears before smaller word length with same starting alphabets (can be many). Do topological sort (post-order DFS, add after visit all nodes), also detect if cycle exists. To check cycle, create a visited map, if node appears in visited with false value, it means it has been visited but not in the path, and set visited[node] = true if node visited and is in the path (that means it forms a cycle). Remember to reverse the order of the string after completing the topological sort.