Problem Sequence
10 Prerequisite LeetCode Problems
My reasoning behind the problems I suggest as preparation for tackling more complex ones. I try to identify problems that involve the key concepts and skills necessary to solve the target problem but are simpler or provide a stepbystep progression in complexity.
Identify key concepts and skills: First, I analyze the target problem to identify the key concepts and skills involved. For instance, for a problem involving sliding windows, these could include the ability to maintain a running total or maximum, understanding of array manipulation, and mastery of a particular data structure like queues or deques.
Begin with the basics: I try to start with problems that provide a gentle introduction to these key concepts and skills. For example, problems that involve basic array manipulations or simpler applications of the sliding window concept.
Increase complexity gradually: Then, I suggest problems that incrementally increase in complexity, often involving more than one of the key concepts or skills, or applying them in more complex or unusual ways.
Cover all key aspects: I aim to suggest problems that collectively cover all key aspects of the target problem. If the target problem involves both sliding windows and deque operations, for instance, I’ll try to include problems focusing on each of these aspects.
Check relevance: Lastly, I ensure that the problems are relevant and appropriate preparation for the target problem. This involves checking problem statements and often the associated discussions or solutions to make sure they involve the identified key concepts and skills and provide suitable preparation for the target problem.
It’s also important to remember that problemsolving is a highly individual process, and different people might find different problems more or less challenging or relevant. The lists I provide are intended as starting points and suggestions, and I’d always recommend tailoring your preparation to your own needs and areas for improvement.
Building the Adjacency List
To manually ensure that there are no cycles while creating an adjacency list for a graph, you can follow these steps:
Unique Nodes: Make sure each problem is unique and doesn’t appear more than once as a key in your adjacency list.
Directed Edges: Ensure that the graph is directed, meaning the relationships only go one way. If problem A depends on problem B, then problem B should not depend on problem A.
No Circular Dependencies: A circular dependency is a cycle in your graph. If problem A depends on problem B, and problem B depends on problem C, then problem C should not depend on problem A. This is because this creates a cycle (A depends on B, B depends on C, and C depends on A).
Transitive Dependencies: Be aware of indirect or transitive dependencies. If problem A depends on problem B, and problem B depends on problem C, then it’s unnecessary (and potentially cyclecreating) to have problem A also directly depend on problem C. The dependency is implied through problem B.
Draw It Out: If you’re unsure or if your graph is small enough, draw it out. Nodes are the problems and edges are the dependencies. Visualizing the graph can help you spot cycles or potential issues.
By following these steps, you can help ensure that your graph is a Directed Acyclic Graph (DAG), suitable for topological sorting.
Enforcing these rules while creating the adjacency list can be a bit tricky because you’re essentially trying to build and validate the graph at the same time. Here are a few guidelines:
Unique Nodes and Directed Edges: These can be enforced by the way you structure your adjacency list. If you’re using a data structure like a dictionary where each key is a problem and the value is a list of dependencies, then each problem will naturally be unique and the relationships will be directed (from problem to dependencies).
No Circular Dependencies: This is the hardest to enforce while building the graph because you need the entire graph to validate it. However, you could run a cycle detection algorithm (like the DFSbased one I mentioned in the previous response) each time you add a new dependency. This would ensure that the new dependency doesn’t create a cycle, but it could be computationally expensive.
Transitive Dependencies: You could potentially check for transitive dependencies each time you add a new one by seeing if the new dependency can be reached by traversing other existing dependencies. However, this could also be computationally expensive and may not be necessary if your main goal is to create a valid graph for topological sorting.
In general, it might be simpler and more efficient to build the adjacency list first (following the rules for unique nodes and directed edges) and then validate the entire graph for cycles and unnecessary dependencies afterwards. This would involve running a cycle detection algorithm on the entire graph and potentially a separate check for transitive dependencies if necessary.
Checking for circular dependencies manually while creating the list can be difficult, especially as the size of the graph grows. However, here are some general steps you can follow:
Keep track of dependencies: When adding a new problem, keep track of all its dependencies. Make sure none of these dependencies directly or indirectly depend on the problem you’re adding.
Visualize the graph: Draw out the graph and the dependencies if it is small enough. This can help you spot any circular dependencies.
Check paths: When adding a new dependency from problem A to problem B, manually check the paths from problem B. If you can reach problem A from problem B through these paths, then adding the dependency would result in a circular dependency.
Remember, these steps are feasible only when dealing with a small graph. For larger graphs, it is recommended to use algorithms like DepthFirst Search (DFS) to detect cycles in the graph. These algorithms are efficient and reliable for cycle detection.
Also, note that you need to check the entire graph for cycles after every addition to ensure that adding a new edge hasn’t created a cycle. This is because a cycle could involve nodes that are not directly connected to the nodes you’re currently working with.
Writing a program to enforce these rules as you build the adjacency list for 2,500 items is indeed possible and feasible.
Here’s a simple Pythonbased outline of how you might go about it:
Initialize an empty dictionary to serve as your adjacency list.
Create a function to add a problem and its dependencies. This function will accept the problem number and a list of problems that it depends on, and it will add them to the dictionary.
Inside this function, before adding the dependencies, run a check to ensure that adding these dependencies will not create a circular dependency. You can use a DFSbased cycle detection algorithm for this.
If the check passes, add the dependencies to the problem in the adjacency list. If the check fails, return an error message and do not add the dependencies.
Call the function to add problems and their dependencies as you build your adjacency list.
Here’s some pseudocode that captures these steps:


Remember, each time you add a new edge, you need to check the entire graph for cycles to ensure that adding the new edge hasn’t created a cycle.
Also, be aware that this kind of cycle detection for each addition can make the program slow if the number of problems and dependencies is large, as the DFSbased cycle detection algorithm has a time complexity of O(V+E) where V is the number of vertices and E is the number of edges. However, for 2,500 items, this should still be quite manageable.
Storing your adjacency list externally in a file is a good idea, especially when dealing with a large number of items. A simple and commonly used format for such data is JSON, due to its compatibility with various programming languages and its readability.
Here is an example of how you could store your adjacency list in a JSON file:


You can load this data into your Python program using the json
module:


And you can write to it like so:


This will allow you to maintain your adjacency list across multiple sessions, and share it easily if needed.
Add error handling to your code to handle cases where the file might not exist, might have incorrect format, etc.
Arranging Problems
Given a sequence of problems or tasks that need to be completed in a specific order, we can represent the dependencies between these problems as a Directed Acyclic Graph (DAG), and further represent this DAG as an adjacency matrix.
In the context of your previous questions, for example, if we consider the sequence of LeetCode problems that should be solved before “Median of a Row Wise Sorted Matrix” as [1, 74, 704, 278, 35, 48, 54, 73, 1198, 378], we can create a 10x10 adjacency matrix where each row and column corresponds to a problem in the sequence.
Since the problems must be solved sequentially (i.e., problem 1 before 74, 74 before 704, etc.), we can represent this by putting a 1 in the (i, j) position of the matrix if problem i must be solved before problem j, and 0 otherwise. Here’s an example of what this might look like:
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
Note that this matrix is very sparse and diagonally dominant, which reflects the sequential nature of the problem dependencies. Also, note that the last row and the first column are all zeros, indicating that the last problem has no subsequent dependencies, and the first problem does not depend on any other problems, respectively.
For a graph with 2500 nodes (problems), it would be more efficient to represent it as an adjacency list instead of an adjacency matrix. This is because an adjacency matrix would take up a space complexity of O(n^2), which is quite large for 2500 nodes. On the other hand, an adjacency list would only take up a space complexity of O(n + m), where n is the number of nodes and m is the number of edges, which would be much more spaceefficient if the graph is sparse.
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph. This collection of lists is the adjacency structure of the graph.
In Python, we can use a dictionary to represent an adjacency list. The dictionary keys would be the nodes, and the dictionary values would be lists of nodes that are directly reachable from the key node.
Here’s a small example:


When you have the adjacency list representation, you can use depthfirst search (DFS) to perform topological sorting. Here’s a simple implementation in Python:


In this implementation, we use a stack to keep track of the nodes in the topologically sorted order. Nodes are only added to the stack after all their dependent nodes have been visited, ensuring that the resulting list is topologically sorted.
So if I provide the unique number of the problem, I will be able to print the sequence of problems that starts with a simple problem and ends with the given problem.
Yes, your understanding is correct. If you’re given a unique problem number, and if you have a graph (represented as an adjacency list) where nodes represent problems and edges represent prerequisite relationships (i.e., problem A must be solved before problem B), you can perform a topological sort on this graph. The resulting sequence of problems will start with a problem that has no prerequisites and end with the given problem, assuming that the given problem is the most complex one with the most prerequisites.
It’s worth noting that topological sorting only works for Directed Acyclic Graphs (DAGs). If there are any cycles in your graph (i.e., circular dependencies between problems), a topological sort cannot be performed. In the context of LeetCode problems, a cycle would mean there’s a loop of problems that all depend on each other, which shouldn’t be the case.
Also, keep in mind that topological sorting is not unique. There can be more than one valid topological sort for a given graph. The exact sequence of problems can vary depending on the order in which nodes and edges are processed, but all valid topological sorts will respect the prerequisite relationships between problems.