LabPractice Week15
LabPractice Week15
April 2025
• Input: richer = [[1, 0], [2, 1], [3, 1], [3, 7], [4, 3], [5, 3],
[6, 3]], quiet = [3, 2, 5, 4, 6, 1, 7, 0]
• Output: [5, 5, 2, 5, 4, 5, 6, 7]
2 * Possible Bipartition
We want to split a group of n people (labeled from 1 to n) into two groups of
any size. Each person may dislike some other people, and they should not be
placed in the same group.
Given the integer n and the array dislikes, where dislikes[i] = [ai , bi ]
indicates that the person labeled ai does not like the person labeled bi , return
true if it is possible to split everyone into two groups in this way, and false
otherwise.
• Input: n = 4, dislikes = [[1, 2], [1, 3], [2, 4]]
• Output: true
1
3 All Paths from Source to Target
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]).
• Input: graph = [[1, 2], [3], [3], []]
• Output: [[0, 1, 3], [0, 2, 3]]
2
• Input:
– board =
ABCE, SF CS, ADEE
– word = ”ABCCED”, ”SEE”, ”ABCB”, ”ABFSAB”, ”ABCD”
• Output:
– For word = ”ABCCED”, returns 1
– For word = ”SEE”, returns 1
– For word = ”ABCB”, returns 1
– For word = ”ABFSAB”, returns 1
– For word = ”ABCD”, returns 0
• Input:
– An integer A, representing the number of nodes.
– A matrix B of size M × 2, where each row represents a directed edge.
• Output:
– Return 1 if the graph contains a cycle.
– Return 0 if the graph does not contain a cycle.
2. The graph does not contain self-loops (edges from a node to itself).
3. There are no multiple edges between any pair of nodes.
4. The graph may or may not be connected.
5. Nodes are numbered from 1 to A.
3
8 Minimum Rolls in Snakes and Ladders
Rishabh is playing Snakes and Ladders and wonders: ”If I could choose the
number I roll on the die, what would be the minimum number of rolls required
to reach the destination square?”
Rules of the Game:
• The game uses a cubic die with 6 faces numbered from 1 to 6.
• The player starts at square 1 and must reach square 100 by rolling the
die. The final roll must land exactly on square 100. If a roll would move
the player beyond square 100, no move is made.
• If the player lands at the base of a ladder, they must climb to its top.
Ladders always go up.
• If the player lands at the mouth of a snake, they must slide down to its
tail. Snakes always go down.
Board Description:
• The board is a 10 × 10 grid with squares numbered from 1 to 100.
• The board contains N ladders, represented as a matrix A of size N × 2,
where each ladder is described as:
(A[i][0], A[i][1])
Here, A[i][0] is the base of the ladder, and A[i][1] is its top.
• The board contains M snakes, represented as a matrix B of size M × 2,
where each snake is described as:
(B[i][0], B[i][1])
Here, B[i][0] is the mouth of the snake, and B[i][1] is its tail.
Your task is to determine the minimum number of rolls required to reach
square 100, assuming you can choose any number between 1 and 6 for each roll.
4
Examples
Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1, you
should have finished course 0. So the correct course order is [0,1].
Example 2:
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3, you
should have finished both courses 1 and 2. Both courses 1 and 2 should be taken
after you finished course 0. So one correct course order is [0,1,2,3]. Another
correct ordering is [0,2,1,3].
Example 3:
Input: numCourses = 1, prerequisites = []
Output: [0]
Examples
Example 1:
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output: 2
Example 2:
Input: times = [[1,2,1]], n = 2, k = 1
Output: 1
5
Example 3:
Input: times = [[1,2,1]], n = 2, k = 2
Output: −1