Model Questions
Model Questions
Graph Questions
Problem: Given the adjacency list and number of vertices and edges of a graph,
the task is to represent the adjacency list for a directed graph.
Examples:
Output: 0 -> 1
1 -> 2
2 -> 0
Example 1:
Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Example 2:
Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
Output: -1
Example:
Input:
Graph= {
0 -> 1
1 -> 2, 5
2 -> 3
3 -> 4
4 -> 0, 1
5 -> null
}
Example :
Input:
n=4
flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]]
src = 0
dst = 3
k=1
Output:
700
Explanation:
The optimal path with at most 1 stops from city 0 to 3 is marked in red and has
cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it
uses 2 stops.
Return the minimum number of minutes required such that none of the cells has
a Fresh Orange. If it's not possible, return -1.
Example:
Input: grid - [ [2,1,1] , [1,1,0] , [0,1,1] ]
Output: 4
Explanation:
Examples:
Input: M[][] = {{‘1’, ‘1’, ‘0’, ‘0’, ‘0’},
{‘0’, ‘1’, ‘0’, ‘0’, ‘1’},
{‘1’, ‘1’, ‘0’, ‘1’, ‘1’},
{‘0’, ‘0’, ‘0’, ‘0’, ‘0’},
{‘1’, ‘0’, ‘1’, ‘1’, ‘0’}}
Output: 5
Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all
possible paths from node 0 to node n - 1 and return them in any order.
The graph is given as follows: graph[i] is a list of all nodes you can visit from
node i (i.e., there is a directed edge from node i to node graph[i][j]).
Example :
Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
Example:
Input :
Output: 0 10 40
Explanation: You can check from the given graph, that all nodes are reachable
from nodes 0, 10, and 40.
10.Course Schedule
There are a total of numCourses courses you have to take, labeled from 0 to
numCourses - 1. You are given an array of prerequisites where prerequisites[i] =
[ai, bi] indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take
course 1.
Return true if you can finish all courses. Otherwise, return false.
Example 1:
Example 2:
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you
should also have finished course 1. So it is impossible.
Tree
For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
Return the total sum of all root-to-leaf numbers.
1. Given the integer depth, for each not null tree node cur at the depth depth - 1,
create two tree nodes with value val as cur's left subtree root and right subtree
root.
2. cur's original left subtree should be the left subtree of the new left subtree root.
3. cur's original right subtree should be the right subtree of the new right subtree
root.
4. If depth == 1 that means there is no depth depth - 1 at all, then create a tree
node with value val as the new root of the whole original tree, and the original
tree is the new root's left subtree.
For each kind of duplicate subtrees, you only need to return the root node of any one of
them.
Two trees are duplicate if they have the same structure with the same node values.
The maximum width of a tree is the maximum width among all levels.
The width of one level is defined as the length between the end-nodes (the leftmost and
rightmost non-null nodes), where the null nodes between the end-nodes that would be
present in a complete binary tree extending down to that level are also counted into the
length calculation.
In a complete binary tree, every level, except possibly the last, is completely filled, and
all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes
inclusive at the last level h.