1462. Course Schedule IV Medium

@problem@discussion
#Depth-First Search#Breadth-First Search#Graph#Topological Sort



1/**
2 * [1462] Course Schedule IV
3 *
4 * There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course ai first if you want to take course bi.
5 * 
6 * 	For example, the pair [0, 1] indicates that you have to take course 0 before you can take course 1.
7 * 
8 * Prerequisites can also be indirect. If course a is a prerequisite of course b, and course b is a prerequisite of course c, then course a is a prerequisite of course c.
9 * You are also given an array queries where queries[j] = [uj, vj]. For the j^th query, you should answer whether course uj is a prerequisite of course vj or not.
10 * Return a boolean array answer, where answer[j] is the answer to the j^th query.
11 *  
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/01/courses4-1-graph.jpg" style="width: 222px; height: 62px;" />
14 * Input: numCourses = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]]
15 * Output: [false,true]
16 * Explanation: The pair [1, 0] indicates that you have to take course 1 before you can take course 0.
17 * Course 0 is not a prerequisite of course 1, but the opposite is true.
18 * 
19 * Example 2:
20 * 
21 * Input: numCourses = 2, prerequisites = [], queries = [[1,0],[0,1]]
22 * Output: [false,false]
23 * Explanation: There are no prerequisites, and each course is independent.
24 * 
25 * Example 3:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2021/05/01/courses4-3-graph.jpg" style="width: 222px; height: 222px;" />
27 * Input: numCourses = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]]
28 * Output: [true,true]
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	2 <= numCourses <= 100
34 * 	0 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)
35 * 	prerequisites[i].length == 2
36 * 	0 <= ai, bi <= n - 1
37 * 	ai != bi
38 * 	All the pairs [ai, bi] are unique.
39 * 	The prerequisites graph has no cycles.
40 * 	1 <= queries.length <= 10^4
41 * 	0 <= ui, vi <= n - 1
42 * 	ui != vi
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/course-schedule-iv/
48// discuss: https://leetcode.com/problems/course-schedule-iv/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53    pub fn check_if_prerequisite(num_courses: i32, prerequisites: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<bool> {
54        vec![]
55    }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_1462() {
66    }
67}
68


Back
© 2025 bowen.ge All Rights Reserved.